Is there a way to execute a custom PHP script inside a meta field?

#13993
  • I’d like to list all my product categories inside the main description of my store page.
    For example:

    “Now our products”: %categorie1%, %categorie2% … %categorie_n&”

    Is it possible?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    Thank you for contacting Rank Math today.

    Yes this is possible, you can create a custom variable replacement that lists all the taxonomy terms using the following filter:

    /**
     * Filter to add custom variables
     */
    add_filter( 'rank_math/vars/replacements', function( $vars ) {
     return $vars;
    });

    Looking forward to helping you. Thank you.

    ​​​​​​

    Right, found in https://rankmath.com/kb/filters-hooks-api-developer/.

    I put this on my themes funcion.php, right?

    But how to get these values inside the page?

    HI Rogerio,

    Thanks for the follow up.

    Yes this will go in your theme’s functions.php file but you will need to come up with some additional arguments to get the products categories and return them when this variable is called.
    Here is an example of how this would look:

    add_action( 'rank_math/vars/register_extra_replacements', function() {
       RankMath\Helper::register_var_replacement( 'Category_list',array('Rank_math_list_cats'),$args = array(
    	        'name'    => esc_html__( 'List of categories', 'rank-math' ),
    			'desc'    => esc_html__( 'Categories from products', 'rank-math' ),
    			'example' => esc_html__(Rank_math_list_cats())));
    	
    	
    } );
    
    function Rank_math_list_cats(){
    	    $list = array();
            $orderby = 'name';
            $order = 'asc';
            $hide_empty = false ;
            $cat_args = array(
               'orderby'    => $orderby,
               'order'      => $order,
               'hide_empty' => $hide_empty,
            );
     
            $product_categories = get_terms( 'product_cat', $cat_args );
          
            if( !empty($product_categories) ){
        
            foreach ($product_categories as $key => $category) {
            $list[] = $category->name;
            }
            }
    	    $cat_list = implode(", ",$list);
    	    return $cat_list;
    	
    
    }

    The code above registers a variable called %Category_list% that will display a comma separated list of categories.

    I hope this info helps. Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)

The ticket ‘Is there a way to execute a custom PHP script inside a meta field?’ is closed to new replies.