• Resolved Jimmy Thai

    (@docteurfitness)


    Hello,

    I would have liked to use the SEO title of Rank Math for a plugin.

    I use this code:

    $seo_title = get_post_meta( $post->ID, ‘rank_math_title’, true );
    if ( ! empty( $seo_title ) ) {
    ? ? $title = $seo_title;
    } else {
    ? ? $title = $post->post_title;
    }

    This works well but for titles with variables, for example: “The best programs %currentyears%”, it does not put the year.

    So I have a link with the following title: The best programs %currentyears%

    Instead of: The best programs 2023

    Can you help me please ?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Jimmy Thai

    (@docteurfitness)

    Chat GPT find this code :

    // Récupérer le titre SEO Rank Math pour le post en cours
    $seo_title = get_post_meta($post->ID, 'rank_math_title', true); // Récupérer les variables de remplacement pour le titre SEO Rank Math
    $variables = get_post_meta($post->ID, 'rank_math_replacements', true); // Convertir les variables de remplacement en tableau associatif
    $variables_array = json_decode($variables, true); if (!empty($seo_title)) {
    // Remplacer les variables dans le titre SEO Rank Math, y compris la variable %title%
    $title = preg_replace_callback(
    '/%([a-zA-Z0-9_-]+)%/',
    function($match) use ($variables_array, $post) {
    $var = $match[1];
    if ($var == 'title') {
    return get_the_title($post->ID);
    } elseif ($var == 'sep') {
    return isset($variables_array['sep']) ? $variables_array['sep'] : '';
    } elseif ($var == 'site_name') {
    return isset($variables_array['site_name']) ? $variables_array['site_name'] : '';
    } elseif ($var == 'page') {
    return isset($variables_array['page']) ? $variables_array['page'] : '';
    } elseif (substr($var, 0, 4) == 'tax_') {
    $tax_slug = substr($var, 4);
    $term = get_term_by('slug', $variables_array[$tax_slug], $tax_slug);
    return is_object($term) ? $term->name : '';
    } else {
    return '';
    }
    },
    $seo_title
    );
    } else {
    // Utiliser le titre de l'article si le titre SEO Rank Math est vide
    $title = get_the_title($post->ID);
    } // Utiliser le titre SEO Rank Math pour le post en cours ou le titre de l'article si le titre SEO Rank Math est vide
    $post_title = $title;

    is it correct for you ?

    Plugin Support Rank Math Support

    (@rankmathteam)

    Hello @docteurfitness,

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

    When fetching meta title from our plugin, the variables inside it must be pre-processed first and you don’t need to manually do that. We have a public helper function to parse the variables for you. Please wrap the returned meta value with this function:

    RankMath\Helper::replace_vars("Some text %title%");

    So your code should end up like this:

    $seo_title = get_post_meta( $post->ID, 'rank_math_title', true ); if ( ! empty( $seo_title ) ) { $title = RankMath\Helper::replace_vars($seo_title); } else { $title = $post->post_title; }

    Kindly try from your end and see if that would work.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get the SEO title’ is closed to new replies.