Custom Variable Not Working in Frontend

#91051
  • Resolved Rajesh
    Rank Math free

    Hi,

    I’m trying to make a custom variable to return current financial year for SEO Title and Meta Description, below is my code

    function FinancialYear(){
    	$year = date('Y');
    	$cyear = substr($year, -2) + 1;
    	return $year.'-'.$cyear;
    }
    add_action( 'rank_math/vars/register_extra_replacements', function() {
       RankMathHelper::register_var_replacement( 'fy',array('FinancialYear'),$args = array(
    	        'name'    => esc_html__( 'Financial Year', 'rank-math' ),
    			'desc'    => esc_html__( 'Return the current Financial year', 'rank-math' ),
    			'example' => esc_html__( '2020-21', 'rank-math' )));
    });

    However, the problem is that, it works perfectly fine within the Admin, i.e. shows the financial year in the preview, but returns nothing in the frontend.

    Preview
    Frontend

    Please help me fix this…

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

    Thank you for getting in touch with us.

    Your filter seems to be missing some elements to completely register the variables with Rank math. To register extra variable replacement, please use the modify the and use the following filter:

    
    /**
     * Action: 'rank_math/vars/register_extra_replacements' - Allows adding extra variables.
     */add_action( 'rank_math/vars/register_extra_replacements', function(){
     rank_math_register_var_replacement(
     'custom_variable_slug',
     [
     'name'        => esc_html__( 'Custom variable name.', 'rank-math' ),
     'description' => esc_html__( 'Custom variable description.', 'rank-math' ),
     'variable'    => 'custom_variable_slug',
     'example'     => 'custom_variable_callback()',
     ],
     'custom_variable_callback()'
     );
    });
    

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Rajesh
    Rank Math free

    Hi Michael,

    Thanks for the quick reply,

    I’ve tried this but with Same results, works on admin, not the frontend.

    Here is my code

    
    function financialyear(){
    	$year = date('Y');
    	$cyear = substr($year, -2) + 1;
    	return $year.'-'.$cyear;
    }
    
    add_action( 'rank_math/vars/register_extra_replacements', function(){
     rank_math_register_var_replacement(
     'fy',
     [
     'name'        => esc_html__( 'Financial Year', 'rank-math' ),
     'description' => esc_html__( 'Returns the current Financial year', 'rank-math' ),
     'variable'    => 'fy',
     'example'     => '2020-21',
     ],
     financialyear()
     );
    });
    Alberto
    Rank Math business

    Hello,

    I think 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.

    Rajesh
    Rank Math free

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    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 4 replies - 1 through 4 (of 4 total)

The ticket ‘Custom Variable Not Working in Frontend’ is closed to new replies.