WordPress.org

Ready to get started?Download WordPress

Forums

how to use shortcodes inside a custom field (9 posts)

  1. tjsherrill
    Member
    Posted 3 years ago #

    I have looked around a bunch and can't find a solution on this.

    I am using custom fields for meta tags. I want to keep things pretty simple. So its just description and keywords. But I setup a shortcode to echo the site description and I want to use the shortcode in the custom field. right now it just prints the shortcode: [bloginfo key='description']

    <meta name="description" content="<?php if(get_post_meta($post->ID, "description", true) !='' ) echo get_post_meta($post->ID, "description", true); else bloginfo('description');?>" />
    <meta name="keywords" content="<?php if(get_post_meta($post->ID, "keywords", true) != '') echo get_post_meta($post->ID, "keywords", true); else echo strtolower(get_bloginfo('name'));?>" />

    Is the code I am using to print out the custom fields. Any help would be greatly appreciated.

    thanks so much

  2. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

  3. tjsherrill
    Member
    Posted 3 years ago #

    Thanks for the help but I am still not 100% on it.

    From my understanding after reading the docs is that:
    <?php echo do_shortcode('[bloginfo key='description']'); ?>

    would print out the shortcode contents. The issue I think I am running into is that I want to plug this shortcode in, in several spots for keyworks. I am using the blog description is a method of geotagging a page, so for example the blog desc in this case is Seattle. So I want to use Seattle several times in the keywords section. So my custom field will look like:

    [bloginfo key='description'] Kites, Where to fly kites in [bloginfo key='description'], Something else about [bloginfo key='description']

    Does that make sense? Is this the best way to get this done?

    thanks

  4. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    does make no sense to me :-(

    can you give more details:

    what is in the custom field?

    what is the full code of the shortcode function?

    (btw: you probably would need to use double quotation marks around your string: <?php echo do_shortcode("[bloginfo key='description']"); ?> )

  5. tjsherrill
    Member
    Posted 3 years ago #

    I am using WP3.0 to create a multi site network of city based sites. Part of the project is making sure that some basic SEO tasks work out of the box. When I create a network site I get to set the site desc to anything I want. So I figured I would tap into that to create a keyword, i.e. Seattle, Portland... The point is not to spam the web at all. The client runs franchise locations in these towns. so the content is 90% the same but its geo tagged.

    I am using a plugin that helps keep my content inline from site to site, and it can include custom fields. So I wanted to setup my custom fields to handle meta desc and meta keywords. So I have custom fields for Description, and Keywords. Then to keep the site as simple to manage as possible, when I create a new sub site I simply propogate the blog description field with a city name. Then I use the shortcode to input that city name across the site anywhere I want. In this case in the custom fields meta tags, I want to be able to reuse the shortcode multiple times.

    Example for Seattle site:
    Meta Description: Seattle based cleanup services
    Meta Keywords: Seattle industrial cleanup services, Seattle Commercial cleanup services, Seattle Hazardous waste cleanup

    But because I am editing the meta tags on the parent site of all these networked sites I have to input the tags like:

    Meta Description: [bloginfo key='description'] based cleanup services
    Meta Keywords: [bloginfo key='description'] industrial cleanup services, [bloginfo key='description'] Commercial cleanup services, [bloginfo key='description'] Hazardous waste cleanup

    in my functions.php

    function bloginfo_shortcode( $atts ) {
       extract(shortcode_atts(array(
           'key' => '',
       ), $atts));
       return get_bloginfo($key);
    }
    add_shortcode('bloginfo', 'bloginfo_shortcode');

    It seems like the problem with the do_shortcode() is that it grabs the shortcode a single time while I need to grab it once in one spot and then 6 times in another spot...

    sorry for the book here, thanks for the help

  6. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    you might be able to do this with a variable for the city name, and a str_replace on a spaceholder (for instance CITY) in your custom fields for descripton and keywords:

    in your example:
    [bloginfo key='description'] Kites, Where to fly kites in [bloginfo key='description'], Something else about [bloginfo key='description']

    you could use this spaceholder instead of the shortcode:
    CITY Kites, Where to fly kites in CITY, Something else about CITY

    and if the city name is always in the site description; then this might work:

    <?php $city = get_bloginfo('description'); ?>
    <meta name="description" content="<?php if(get_post_meta($post->ID, "description", true) !='' ) echo str_replace('CITY', $city, get_post_meta($post->ID, "description", true) ); else bloginfo('description');?>" />
    <meta name="keywords" content="<?php if(get_post_meta($post->ID, "keywords", true) != '') echo str_replace('CITY', $city, get_post_meta($post->ID, "keywords", true) ); else echo strtolower(get_bloginfo('name'));?>" />
  7. tjsherrill
    Member
    Posted 3 years ago #

    That looks great. Do I need to define CITY in the functions.php file at all? or does the custom field know that CITY = $city ?

    this has been a huge help and its getting me over one of the larger bumps in this project.

    thanks

  8. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    CITY = $city

    as long as you use CITY in your custom fields, it does this in the string replace function.

    this line gets the city name from the bloginfo description:

    <?php $city = get_bloginfo('description'); ?>

    and this part of the code would replace the placeholder CITY with the city name in the custom fields, for instance:
    str_replace('CITY', $city, get_post_meta($post->ID, "description", true) );

    http://php.net/manual/en/function.str-replace.php

  9. tjsherrill
    Member
    Posted 3 years ago #

    Works like a charm. Can't say thanks enough

Topic Closed

This topic has been closed to new replies.

About this Topic