Change meta ttitles and descriptions with PHP

#127511
  • Resolved Erez Speiser
    Rank Math free

    I have a page that is created dynamically with PHP (with a ? URL parameter)
    It is implemented as a custom template in my WP child theme.
    I would like to change the Meta Title and Descriptions dynamically.

    Can you guide me on how to do it?

    Many thanks!

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

    Thank you for contacting Rank Math and bringing your concern to our attention.

    You can use and customize our filter to change the meta title and description in the links below:
    https://rankmath.com/kb/filters-hooks-api-developer/#change-the-title
    https://rankmath.com/kb/filters-hooks-api-developer/#change-meta-description

    I hope that helps. Thank you, and looking forward to your update.

    Thank you for your reply.
    I added following code to my custom PHP template file:

    $title="override title";
    add_filter( 'rank_math/frontend/title', function( $title ) {
        return $title;});
    
    $$description="custom description";
    add_filter( 'rank_math/frontend/description', function( $description ) {
        return $description;
    });

    But nothing happens. It keeps using the original parameters defined on the page.

    Any ideas?

    Thanks
    Erez

    Hello,

    Thank you for the follow up.

    Please try it as follows:

    
    add_filter( 'rank_math/frontend/title', function( $title ) {
        $title="override title";
     return $title;
    });
    
    add_filter( 'rank_math/frontend/description', function( $description ) {
       $description="custom description";
        return $description;
    });
    

    Hope this helps you. Thank you.

    Hi Michael,

    Thanks for your prompt reply.
    Still does not work.
    When I check the source of the page after it loads I still get the original text I saved with the plugin on the page.
    I managed to modify the tags with JS after page load. But I prefer that the page will load with the right meta and title from the beginning.

    What’s next?
    Thanks
    Erez

    Hello,

    Thank you for contacting Rank Math again and sorry for the delay.

    Since the filter hooks are not working on your site, we might need to take a closer look at your site and we need the login information to do that.

    Please edit the first post on this ticket and include your WordPress & FTP 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.

    Thank you.

    Hello,

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

    Thank you.

    I submitted the credentials in the sensitive data area.

    The template where I am trying to implement this is:
    /public_html/wp-content/themes/astra-child/templates/mds_test.php
    the relevant filter code is in lines 75-82
    The page that uses the template is: https://www.machiningdoctor.com/mds-test/

    Many thanks for your dedicated support

    Erez

    Hi,

    The code can be added just to the functions.php file. Since you only need the changes on your custom template, I modified the code to the following:

    
    add_filter( 'rank_math/frontend/title', function( $title ) {
    		if ( is_page_template( 'templates/mds_test.php' ) ) {
    		$title="override title";
    	 return $title;
    		}
    	 return $title;
    	});
    
    	add_filter( 'rank_math/frontend/description', function( $description ) {
    		if ( is_page_template( 'templates/mds_test.php' ) ) {
    	   $description="custom description";
    		return $description;
    		}
    		return $description;

    This works on your test page, you can confirm the same in the page source, check the screenshot I have added in the sensitive section.

    Hope this helps. Thank you

    Hi

    Thanks for that. I checked and it works!
    However, my problem is that I need to set the title and meta according to values calculated in the template PHP code that depends on values pulled from the database.
    These values are still unknown when the function.php file loads?

    Any ideas on how to overcome this?

    Thanks
    Erez

    Hello,

    You can try calculating your values using a global function that returns your final value which you can then try calling from your functions.php.

    I will suggest that you get in touch with a developer to assist you in implementing this as code customization is something we do not offer.

    Hope this helps

    I understand.
    Thank you all for your dedicated support.

    You can close the ticket.

    Hello,

    We are super happy that we were able to assist you with your concern. 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/#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    If you 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 12 replies - 1 through 12 (of 12 total)

The ticket ‘Change meta ttitles and descriptions with PHP’ is closed to new replies.