Retrieve title after AJAX call

#458267
  • Resolved Frederik Delmotte
    Rank Math free

    How can I retrieve the title value after an AJAX call?

    RankMathPaperPaper::get()->get_title();

    The above code does not work when I bring in the content dynamically.

    Thank you.

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

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

    Please note that when requesting an AJAX operation, you need to call the path /wp-admin/admin-ajax.php as your ajaxurl.

    Please refer to this example setup to perform an AJAX operation in WordPress:

    PHP:
    https://pastebin.com/1u63MZds

    HTML/JS:
    https://pastebin.com/0f7J6SGg

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

    I already have a full working website with AJAX (using /wp-admin/admin-ajax.php). The issue is that RankMath\Paper\Paper::get()->get_title(); doesn’t return a value when I use this line in the callback. Is there a way I can pass the post ID? RankMath\Paper\Paper::get()->get_title($post->ID); doesn’t seem to work.

    Hello,

    Please share some screenshots of the code you have created so we can analyze advice accordingly. Possibly, please share the code you made from the JS side and the PHP side.

    Looking forward to helping you on this one.

    Sure thing, please see screenshots attached. Again, I do have a full working website. I assume I just need to load RankMath in the PHP callback, but don’t know how…

    Nigel
    Rank Math business

    Hello,

    Please add the following line below the line $shareTitle = RankMath\Paper\Paper::get()->get_title();

    
    echo $shareTitle;
    

    Hope that helps. Please let us know if you have questions.

    I’m sending $shareTitle as JSON:

    $shareTitle = RankMath\Paper\Paper::get()->get_title(); 
    wp_send_json_success( array( 'html' => $html, 'title' => $shareTitle ) );

    and getting it back with javascript:

    response.data.title

    I’ll try to explain this again. If I have a single.php file in WordPress and have this line of code:

    <p> <?php echo RankMath\Paper\Paper::get()->get_title(); ?> </p>

    This will get the me the page title when I browse to this page/URL directly.
    However, when I load single.php with AJAX, the page title does not appear. How can I bring in the RankMath plugin and all its functions when there is no header/footer from WordPress?

    After a bit of digging it seems that the get_title function returns an empty string (see latest screenshot) when called with AJAX. I assume the filter in the get_papers function in class-paper.php isn’t being set properly in this scenario. Anything I can do to resolve this?

    After even more digging, it seems the post object is not being updated. How can I set the post ID for RankMath to understand the post has changed? The setup() function in class-paper returns the first ID when the site was loaded. Thanks

    Hello,

    The Paper class will only work when the is_singular() condition returns true. In ajax callback, this condition will return false. Instead of using Paper class, please try using the following code to get the SEO Title:

    
    $post_id = 1;
    $title = get_post_meta( $post_id, 'rank_math_title', true );
    if ( '' === $title ) {
        $post_type = get_post_type( $post_id );
        $title = RankMath\Paper\Paper::get_from_options( "pt_{$post_type}_title", get_post( $post_id ), '%title% %sep% %sitename%' );
    }
    

    I hope that helps.

    Thanks for the reply, unfortunately RankMath\Paper\Paper::get_from_options(…) only returns the separator. get_post_type and get_post does return values on a AJAX call. Is there a way we can load the title without the Paper class?

    Hello,

    An alternative to Paper class is using Rank Math’s Helper function to get the default title from settings. Please try this code:

    
    $post_id = 1;
    	$title = get_post_meta( $post_id, 'rank_math_title', true );
    	if ( '' === $title ) {
    		$post_type = get_post_type( $post_id );
    		$title = RankMath\Helper::replace_vars( RankMath\Helper::get_settings( "titles.pt_{$post_type}_title", '%title% %sep% %sitename%' ), get_post( $post_id ) );
    	}
    

    I hope that helps.

    Still no result I’m afraid. I’m just getting the separator back with the helper function as well. Is there a working solution for the AJAX scenario using RankMath or should we look for alternatives?

    Hello,

    If the value is still returning empty, then you may need to “setup” the variables before using the replace_vars() function. Please try this instead:

    $post_id = 1;
    $title = get_post_meta( $post_id, 'rank_math_title', true );
    if ( '' === $title ) {
    	$post_type = get_post_type( $post_id );
    	rank_math()->variables->setup();
    	$title = RankMath\Helper::replace_vars( RankMath\Helper::get_settings( "titles.pt_{$post_type}_title", '%title% %sep% %sitename%' ), get_post( $post_id ) );
    }

    Let us know how that goes. Looking forward to helping you.

    Amazing, thank you so much Jeremy. The setup works with both the paper and helper function. Appreciate all the help!

    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/#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 15 replies - 1 through 15 (of 15 total)

You must be logged in to reply to this ticket.