• Resolved tekgirlymama

    (@tekgirlymama)


    Hi and many thanks for your great plugin.

    I am trying to get shortcodes to work in it – Meta title, description, OG and Twitter etc.

    What am I doing wrong..?

    I use a plugin “my custom functions” and can see results OK in post title and content but not in post excerpt nor in SEO plugin fields.
    Please advise. I hunted online 2 days and can’t find a solution ??

    This is my code –

    add_filter(‘widget_text’, ‘do_shortcode’);

    //* Activate shortcode function in SEO Autodescription plugin Title and Meta Description
    add_filter( ‘autodescription_title’, ‘do_shortcode’ );
    add_filter( ‘autodescription_description’, ‘do_shortcode’ );

    //* and in Autodescription OG and Twitter titles and descriptions
    add_filter( ‘autodescription_og_title’, ‘do_shortcode’ );
    add_filter( ‘autodescription_og_description’, ‘do_shortcode’ );
    add_filter( ‘autodescription_twitter_title’, ‘do_shortcode’ );
    add_filter( ‘autodescription_twitter_description’, ‘do_shortcode’ );

    //* Shortcode to DYNAMICALLY display the CURRENT year
    //* shortcode: [year]
    add_shortcode( ‘year’ , ‘current_year’ );
    function current_year() {
    $year = date(“Y”);
    return “$year”;
    }

    and for other shortcodes like [month] etc.

    In post source I see [year] instead of the value 2019 (as example) in head meta.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hello ??

    I put various examples on our site that enable the exact behavior you’re looking for.

    To summarize, this is all the code you need, including a safer (no code conflicts & escaped) shortcode than what you have there:

    // Registers a safer [title-year] shortcode.
    add_shortcode( 'title-year', function() {
    	return esc_html( date( 'Y' ) );
    } );
    
    // Enables shortcode parsing for the title
    add_filter( 'the_seo_framework_title_from_custom_field', function( $title ) {
    	return $title ? do_shortcode( $title ) : $title;
    } );
    
    // Enables shortcode parsing for the custom meta description
    add_filter( 'the_seo_framework_custom_field_description', function( $description ) {
    	return $description ? do_shortcode( $description ) : $description;
    } );

    The Open Graph and Twitter fields don’t have filters attached to them. But you can forward this to the output, beware of plausible future discrepancy in the admin area:

    // Enables shortcode parsing for the og:description output.
    add_filter( 'the_seo_framework_ogdescription_output', function( $description ) {
    	return do_shortcode( $description );
    } );
    
    // Enables shortcode parsing for the twitter:description output.
    add_filter( 'the_seo_framework_twitterdescription_output', function( $description ) {
    	return do_shortcode( $description );
    } );

    I hope this helps ?? Cheers!

    • This reply was modified 6 years ago by Sybre Waaijer. Reason: formatting
    Thread Starter tekgirlymama

    (@tekgirlymama)

    Thank you Sybre for your detailed and fast reply on the weekend even!

    I checked out the page on your site for API info.
    What is the difference in results?

    from your page info, Run shortcodes in the custom title:

    add_filter( ‘the_seo_framework_title_from_custom_field’, function( $title ) {
    return do_shortcode( $title );
    } );

    However, above you provided this code:

    // Enables shortcode parsing for the title
    add_filter( ‘the_seo_framework_title_from_custom_field’, function( $title ) {
    return $title ? do_shortcode( $title ) : $title;
    } );

    ***********
    Both my [year] and your recommended safer shortcode [this-year] work in custom meta title now ?? –

    <title>Private: custom Meta title in seo framework – 2019 or 2019</title>
    <meta name="description" content="This is seo framewok’s Meta description. Let’s test if 2019 or the 2019 and also 31st March 2019 work in meta. And todays date is… March 31, 2019" />

    (BTW, how to get rid of display of ‘framework & # 8 2 1 1 ;’ instead of my input ‘framework -‘ or ‘framework’s’ does it not matter for SEO? Would be nicer having clean meta without unicode characters or whatever you call them)

    ******

    And good news OG (and Twitter) work fine but how to get title OG to work?

    <meta property=”og:title” content=”OG custom title with shortcode [year] or [this-year]” />
    <meta property=”og:description” content=”OG description with date shortcode: 31st March 2019 or 2019″ />

    Would I add this (seems to work just fine!):

    // Enables shortcode parsing for the og:title output.
    add_filter( ‘the_seo_framework_ogtitle_output’, function( $description ) {
    return do_shortcode( $description );
    } );

    // Enables shortcode parsing for the twitter:title output.
    add_filter( ‘the_seo_framework_twittertitle_output’, function( $description ) {
    return do_shortcode( $description );
    } );

    **********

    The last question is, if one needs to do custom OG and Twitter titles and descriptions at all. What’s the benefit and use of doing so? When would you suggest it is a good idea to make OG and Twitter custom inputs?

    If I just stick to main SEO title and meta description, I see OG etc inherits it from main settings.

    Many thanks for your time and effort!

    • This reply was modified 5 years, 12 months ago by tekgirlymama.
    Thread Starter tekgirlymama

    (@tekgirlymama)

    Part 2: Please would you show me how to turn the following 3 shortcodes to a safer equivalent as you did for [year]?

    // Shortcode for Dynamic Current Month
    // shortcode: [month]
    add_shortcode( ‘month’ , ‘current_month’ );
    function current_month() {
    $month = date(“F”);
    return “$month”;
    }

    //* Shortcode to add a Dynamic CURRENT DATE like: January 22, 2016 – from WP General settings F j, Y
    //* shortcode: [datetoday]
    add_shortcode( ‘datetoday’, ‘displayTodaysDate’);
    function displayTodaysDate( $atts ) {
    return date(get_option(‘date_format’));
    }

    // Shortcode for DYNAMIC CURRENT DATE – can change format here
    // shortcode: [date]
    add_shortcode( ‘date’ , ‘current_date’ );
    function current_date() {
    $date = date(“jS F Y”);
    return “$date”;
    }

    Plugin Author Sybre Waaijer

    (@cybr)

    Hello again!

    The difference from here and there is that the one on my site is for educational purposes, and the one I posted here is a real-world execution.

    If I add too many conditions (ifs and elses) on the educational front, users will have to question the implementation, which adds much to confusion if they’re not savvy with PHP or critical programming.

    To explain the actionable difference:
    The custom meta field doesn’t always have content in it. To run an expensive, although optimized function (do_shortcode()) over an empty field is a waste of resources. So, we first assess if there’s content, and then run the function.

    Let’s move on to your other questions…

    How do I remove HTML symbols, like &#8211; (–)
    You can’t. This is a result of a low-entropy security feature. With that, I mean that there’s a net-loss of near zero from your input to the actual output. The symbols closely resemble the ones you’ve put in (they’re “beautified”), and it won’t affect SEO ranking.

    It may cause the pixel counter to be off by a few pixels, which, on edge-cases, will cause the title or description to overflow regardless.

    og:title and twitter:title tag support
    Oops! My bad, I forgot to include those. I see you’ve already found them. Just to affirm, here you go:

    // Enables shortcode parsing for the og:title output.
    add_filter( 'the_seo_framework_ogtitle_output', function( $title ) {
    	return do_shortcode( $title );
    } );
    
    // Enables shortcode parsing for the twitter:title output.
    add_filter( 'the_seo_framework_twittertitle_output', function( $title ) {
    	return do_shortcode( $title );
    } );

    When should I utilize the Twitter and Open Graph inputs?
    Twitter and Facebook have different title-and description truncation standards to Google. You may want to account for those; but, more importantly, if you think you can cater to the interest of your Twitter and Facebook followers with different inputs, then they’re there for you.

    For me, I often don’t even bother. But that’s because my target audience isn’t pronounced on either of those sites.

    You made my shortcode “safer”, what did you do?
    I made it safer by changing the name and (redundantly) escaping the output. current_date() looks a lot like a standard PHP or WordPress function; may they ever add such a function internally, then your code won’t collide and crash.

    So, I discarded the function name completely, and made it an anonymous lambda function:
    some_call( function() { /* this is the lambda function, as it has no name */ } );

    The same principle is followed for the shortcode; I added a prefix (title-) to it; to prevent it from ever colliding with a WordPress, theme, or plugin update.

    So, before you stretch me thin asking to improve your code: Go and play with them yourself, you may learn a thing or two ??

    I hope this all helps! Have a beautiful day!

    • This reply was modified 5 years, 12 months ago by Sybre Waaijer. Reason: formatting
    Thread Starter tekgirlymama

    (@tekgirlymama)

    Thank you very much, Sybre!
    My bad, I had title calling for description, must stop working into late hours, lol.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘shortcode support’ is closed to new replies.