2

So, I know you can put:

function page_title_sc( ){
   return get_the_title();
}
add_shortcode( 'page_title', 'page_title_sc' );

Into function.php to fetch the page title and get a [page_title] shortcode within Wordpress, but is it possible to do exactly that for keywords?

I put a focus keyword down in Yoast SEO and I would like to have a shortcode for that.

Or another idea: is there a way to have a custom shortcode field in every page? So that I only have to put something into that once and can use it as a shortcode within the whole page?

2
  • Can I ask your use case for this? You say you have a focus keyword in Yoast, are you trying to put this keyword in the content? Commented Apr 3, 2020 at 18:38
  • Yeah, basically I want to fetch the keyword of every page, so that I don't have to manually type it up every time. Commented Apr 3, 2020 at 18:45

3 Answers 3

1

If you want a shortcode for Yoast to output the keyphrase automatically:

function wpso_61018203_output_yoast_keyphrase() {
    // Make sure Yoast is installed/active.
    if ( class_exists( 'WPSEO_Meta' ) ) :
        // Hold the global post object.    
        global $post;

        return WPSEO_Meta::get_value( 'focuskw', $post->ID );
    endif;
}

add_shortcode('yoast_kw', 'wpso_61018203_output_yoast_keyphrase' );

You can then do this ['yoast_kw'] in your content, or use echo do_shortcode('[yoast_kw]'); in your template.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, that's exactly what I was looking for! Do you happen to know whether it's also possible to set a custom word per each page and have that as a shortcode?
0

Use get_post_meta and grab _yoast_wpseo_focuskw:

function page_focus_keyword( ){
   return get_post_meta(get_the_ID(), '_yoast_wpseo_focuskw');
}
add_shortcode( 'focus_keyword', 'page_focus_keyword' );

Comments

0

WordPress filters all content to make sure that no one uses posts and page content to insert malicious code in the database. This means that you can write basic HTML in your posts, but you cannot write PHP code.

And you want to use Wordpress shortcode for focus keyword.

Possible solution given on Wordpress.org Yoast SEO plugin support query on similar query, and checkout the linked solution

insert the below php snippet in functions.php

if(!function_exists('wpseoFocusKW'))
{
        function wpseoFocusKW()
        {
            $focuskw = wpseo_get_value('focuskw', $post->ID);
            echo $focuskw;
        }
}

And to use the shortcode in another page for focused keyword, insert the below shortcode in any pages with yoast-seo plugin :

Shortcode for focus keyword

 [<?php wpseoFocusKW();?>]

1 Comment

That's not a shortcode. That's a function. OP wants to use the output within the content. I'm not going to down vote this, but this does not answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.