0

I have a wordpress site and I am using a plugin that uses shortcode as a widget to pull recent blog posts. In the column I am getting an error that reads:

Warning: Illegal string offset 'title' in /home3/xxxxx/xxxx.com/wp-content/plugins/recent-blogs-shortcode/recent-blogs-shortcode.php on line 44

So the Title would be the blog post title, but I am just having trouble on how to fix it.

add_shortcode( 'recent-blogs', 'cp_sidebar_recent_blogs_shortcode');

function cp_sidebar_recent_blogs_shortcode( $atts, $content = null ) {

// shortcode_atts will only allow us to supply 1 default; for the horizontal layout submitted with no title, use a different default
$title_default = '';
if ($atts['title'] == 'Related Posts' && $atts['layout'] == 'horizontal' ) {
    $title_default = 'Related Posts';
}
else {
    $title_default = 'Recent Posts';
}

1 Answer 1

0

The answer here is assumed the user equipped with the following knowledge

  • PHP and know how to find reference from php manual online
  • WP built-in shortcode_atts() - Combine user attributes with known attributes and fill in defaults when needed.

I guess you haven't written the shortcode in a proper form.

Illegal string offset 'title' means $atts['title'] does not exist. It does not exist because when shortcode is being called, title is not given

To resolve this, you need to set default values and then combine it with user attributes(the attributes supply when anyone call the shortcode).

Here is a recommended writing style for a shortcode

add_shortcode( 'recent-blogs', 'cp_sidebar_recent_blogs_shortcode');
function cp_sidebar_recent_blogs_shortcode( $atts, $content = null ) {

    // the following is default value + merge value from shortcode calling, 
    // it can prevent errors in case any attribute is empty such as title is not defined
    extract(shortcode_atts(array(
        'title' => '',
        'layout' => '',
    ), $attr));

    // with extract() you don't need to explicitly create a $title_default
    // because extract() will create $title with value ''

    if ( $atts['title'] == 'Related Posts' && $atts['layout'] == 'horizontal' ) {
        $title = 'Related Posts';
    }
    else {
        $title = 'Recent Posts';
    }

    // ... your other code continue
}
4
  • S- ty so much for your response. I wanted to ask you another question. Perhaps the code below what I left out might be helpful. I tried it with your writing style and got the same error. Can you tell me if this code pertains to anything in reference to above $a = shortcode_atts( array( 'title' => $title_default, 'count' => 1, 'excerpt' => TRUE, 'category' => false, 'wrapper_class' => 'recent-blogs-wrapper', 'layout' => 'horizontal', 'bg_color' => get_option('accent_banners_bg_colors_picker', 'fresh-blue' ) ), $atts ); Apr 14, 2020 at 1:54
  • When you test a new code, try to be simple. One parameter at a time to make sure things is in the right place and make sure you understand what the meaning of code is. Don't hurry. It takes time. Too much variables to test a newly learnt code without thorough understanding will mislead yourself. Apr 14, 2020 at 1:59
  • Thank you.. I am trying to learn this. See this was a working plugin before, but when I migrated my site to a new host it broke. So the code works, just something is broken to not be working anymore., The developer is not reachable, so I am trying to learn how to fix this code myself. I really do appreciate your response, where would be the a place where I can learn Apr 14, 2020 at 3:00
  • Hope it helps and happy coding. While sometimes it is tedious, the process is fun. When you use a function, you may like to pay attention of the return value. And then use php built-in functions like var_dump or print_r to see what is the output of a broken variables. In this way, you will have better picture of which part is wrong. Apr 14, 2020 at 3:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.