Register a custom template variable

#5415
  • Resolved Aesell Classifieds
    Rank Math free

    Dear Team,

    I am creating this support topic as a follow up from the facebook post ( https://www.facebook.com/groups/rankmathseopluginwordpress/permalink/358873651651634/ )

    I have followed your instruction and found the output was “Undefined” (When I inserted the variable “%ListingCat%” in the meta description field

    Following is the code that I inserted in the fuctions.php file (Which was given by theme auther for Yoast plugin and It was working in yoast – and customised for RankMath by instructed but not worked)

    add_action( 'rank_math/vars/register_extra_replacements', function() {
       RankMath\Helper::register_var_replacement( 'listingCat', 'wilcityAddPrimaryCategoryToListingCallback', 'advanced');
    } );
    
    function wilcityAddPrimaryCategoryToListingCallback(){
       $aDirectoryTypes = \WilokeListingTools\Framework\Helpers\GetSettings::getAllDirectoryTypes(true);
       if ( !is_singular($aDirectoryTypes) ){
          return '';
       }
       global $post;
    
       $termID = \WilokeListingTools\Framework\Helpers\GetSettings::getPrimaryTermIDOfPost($post->ID, 'listing_cat');
       if ( !empty($termID) ){
          $oTerm = get_term($termID, 'listing_cat'); 
          if ( !empty($oTerm) && !is_wp_error($oTerm) ){
             return $oTerm->name;
          }
       }
    
       return '';
    }

    Ref Image :
    Screenshot

    Kindly do the needfull, If you need any further details, Please let me Know.

    NB: Yours is very wonderful Plugin !

    • This topic was modified 5 years, 7 months ago by Aesell Classifieds. Reason: Add custom variable on 19-05-2019
Viewing 9 replies - 1 through 9 (of 9 total)
  • Hello,

    Thank you for contacting Rank Math and sorry for any inconvenience that might have been caused due to that.

    Please try the following code and let me know how this goes:

    add_action( 'rank_math/vars/register_extra_replacements', function() {
       RankMath\Helper::register_var_replacement( 'listingCat', 'wilcityAddPrimaryCategoryToListingCallback', $args = array(
    	        'name'    => esc_html__( 'Listing Category', 'rank-math' ),
    			'desc'    => esc_html__( 'Category of the current listing', 'rank-math' ),
    			'example' => esc_html__( 'Example Category', 'rank-math' )));
    } );
    
    function wilcityAddPrimaryCategoryToListingCallback(){
       $aDirectoryTypes = \WilokeListingTools\Framework\Helpers\GetSettings::getAllDirectoryTypes(true);
       if ( !is_singular($aDirectoryTypes) ){
          return '';
       }
       global $post;
    
       $termID = \WilokeListingTools\Framework\Helpers\GetSettings::getPrimaryTermIDOfPost($post->ID, 'listing_cat');
       if ( !empty($termID) ){
          $oTerm = get_term($termID, 'listing_cat'); 
          if ( !empty($oTerm) && !is_wp_error($oTerm) ){
             return $oTerm->name;
          }
       }
    
       return '';
    }

    Looking forward to helping you. Thank you.

    ​​​​​​​

    It is giving the result “Example Category”
    Should I replace any of the texts ?

    'name'    => esc_html__( 'Listing Category', 'rank-math' ),
    			'desc'    => esc_html__( 'Category of the current listing', 'rank-math' ),
    			'example' => esc_html__( 'Example Category', 'rank-math' )));

    Thanks.

    Hi there,

    Thanks for the follow up.

    This suggests that you custom callback is not firing and this is why it is falling back to the example.

    We might need to take a closer look at the settings. Please edit the first post on this ticket and include your WP logins in the designated Sensitive Data section.
    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    We really look forward to helping you.

    Hello,

    I have updated the sensitive data as requested. Can you please check further?
    You can find the code at
    Appearance > Theme Editor > Function.php (Child Theme)

    Thank you.

    Hello,

    I see the code is showing proper category term on the frontend. Only on the backend its showing Example Category and it’s probably because the class which you are using to get primary term value is initialized only on the frontend.

    Can you please contact the theme author and ask them if there is any other way to get the primary term name in the admin panel?

    Looking forward to helping you. Thank you.

    1 ) How can I varify that the code is showing proper category ?
    2) For Yoast, It was working in backend so I could get an Idea, how to make the description Useful. I don’t think they will support for the third party plugin.
    3) I Have other three more variables to add if its truely working. anyways I can add it now. could you please check its error free and I’m good to go.

    Really appreciate your support !
    Sajesh

    HI there,

    Thanks for the follow up.

    I have modified your code to the below to work for the backend too:

    add_action( 'rank_math/vars/register_extra_replacements', function() {
       RankMath\Helper::register_var_replacement( 'listingCat',array('RMPrimaryCategoryToListingCallback'),$args = array(
    	        'name'    => esc_html__( 'Listing Category', 'rank-math' ),
    			'desc'    => esc_html__( 'Category of the current listing', 'rank-math' ),
    			'example' => esc_html__(RMPrimaryCategoryToListingCallback())));
    	
    	
    } );
    
    function RMPrimaryCategoryToListingCallback(){
    	
    	$post_id = $_GET['post'];
    	$termID = \WilokeListingTools\Framework\Helpers\GetSettings::getPrimaryTermIDOfPost($post_id, 'listing_cat');
    	if ( !empty($termID) ){
          $oTerm = get_term($termID, 'listing_cat'); 
          if ( !empty($oTerm) && !is_wp_error($oTerm) ){
             return $oTerm->name;
          }
       }
    
    }

    I hope this helps. Thank you.

    Wow, That was really awsome !!!!!! Thank you so much !!!!!!

    Hello,

    We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.

    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/?rate=5#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

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

The ticket ‘Register a custom template variable’ is closed to new replies.