• Hello,

    I am using the core text widget on my site. In it, I have a shortcode that will display the EXIF data of the image on that specific post. But, some of my images don’t have EXIF data. When that happens, the title of the text widget (EXIF) still appears in the sidebar. I want to have the widget (the whole thing if possible, but at least the title) not display if there is no EXIF data to show so there is no title with nothing under it.

    I’ve been searching the web and trying a variety of things for several hours and I can’t find the solution. I’m hoping that someone might be able to assist me.

    Thanks.

    • This topic was modified 4 years, 9 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 14 replies - 1 through 14 (of 14 total)
  • The widget title is output by WP core (because of the theme), but the shortcode is totally separate.

    I would remove the widget title and if you want a title, change the shortcode to output it.

    Thread Starter Josh

    (@joshmbuck)

    I’m sorry, I don’t know much about coding. Are you saying that it’s the theme that prevents me from hiding the title when there is no information in the widget? If that’s the case, shouldn’t I be able to tweak my theme?

    If you’re saying there is a shortcode that allows me to hide the widget title from within the widget (vs going into the php files), I would very much appreciate that shortcode.

    Thanks.

    No, you completely misunderstood what I said.
    The theme makes a call to WordPress function to output the sidebar. It doesn’t have any information about what widgets are in the sidebar or if they have content. The WordPress function outputs the title, the same for every widget. It is not concerned with the widget content. The title is independent of the content of the widget. The content, in your case this shortcode, has nothing to do with the widget title. They are independent.
    So to remedy the fact that they are independent, output them together. The easiest way to output them together is to remove the title from the widget since that is handled by WP functions, and have the shortcode itself output the title, because that’s the part that knows if there is content to output.

    Thread Starter Josh

    (@joshmbuck)

    This is a bit complicated. I had assumed there was some kind of if/then statement (if there is data in the widget, then print title, otherwise no title), but if I’m understanding what you are saying there is no way for WordPress to know if there is info in the widget, so it will always print the title.

    to get around this, don’t put in a title in the Widgets section of the admin menu and just put it in the body of the widget itself by using a shortcode.

    if that is correct, how do i know what the shortcode is?

    Didn’t you put the shortcode into the widget, so you know what it is?
    If it’s one you built with a shortcode plugin, you can modify it.
    If it’s one supplied by a plugin, you can ask at the plugin’s support forum, or simply make a copy of that plugin and modify the code to output the title. Or copy the shortcode handler function to one of those plugins you can make your own shortcode, and rearrange the output to your liking.

    Thread Starter Josh

    (@joshmbuck)

    The shortcode I put in is from the plugin…it just lists the EXIF data from the image in the post. The plugin doesn’t have it’s own widgets, so I just copied and pasted the shortcode into the text widget that’s built into WordPress. It doesn’t seem that the plugin has any additional shortcodes to control anything beyond printing the EXIF data.

    I have no idea how to create a shortcode.

    It seems that I will not be able to get it to do what I wanted it to. Oh, well. I really thought this would be a lot simpler than it appears to be.

    Thread Starter Josh

    (@joshmbuck)

    OK, so digging around the plugin’s forum, I did in fact find some coding to create a shortcode, but it doesn’t have anything to create a title within the widget. Can you help me out with that bit of coding? Then I think I’ve got it.

    Here’s what I have so far:

    // creates a shortcode to show EXIF outside the loop
    add_shortcode( 'widget_exif' , 'exif_outside_loop' );
    function exif_outside_loop() {
    	global $wp_query;
    	$post_ID = $wp_query->post->ID;
    	$img = get_post_thumbnail_id($post_ID);
    	if (function_exists ('exifography_display_exif'))
    	echo exifography_display_exif($img);
    }
    
    // enables shortcodes within widgets
    add_filter('widget_text', 'do_shortcode');

    Note that the function get_post_thumbnail_id is for the featured image.
    Shortcodes should not echo. Change echo to return and it will be good.
    However, to output the title, you need to return it also, and you need to check if there is any EXIF data before you do that.
    Since you need to return something always, set the variable to empty string before the if statement. Use braces for a multi-line if statement.

    $exif ==''; 
     if (function_exists ('exifography_display_exif')) {
        $exif = exifography_display_exif($img);
        if ( !empty($exif) ) $exif = '<h3>Exif title</h3>' . $exif;
      }
    return $exif;

    I don’t quite understand why you are writing a shortcode when you already had one, but whatever.

    Thread Starter Josh

    (@joshmbuck)

    thanks.

    the shortcode I have is

    [exif show="camera,focal_length,shutter_speed, aperture,iso,flash,location"]

    If you can tell me how that can make it so there is no title when there is no data, that would be great. But you suggested writing a shortcode, so I was able to find what I posted above, and hopefully if I can figure out how to add what you just gave me, it will work the way I want.

    As I stated, I don’t know anything about this stuff…I’m just trying to figure it out as I go. I have no education or training in coding.If there is an easier way to make this work, I’m very interested to know.

    • This reply was modified 4 years, 9 months ago by Josh.

    My thinking was that you already have a plugin with a shortcode, and you could either modify the plugin’s code or copy the plugin and make your changes in the copy. But writing your own shortcode to call the existing handler works also, I guess.

    Thread Starter Josh

    (@joshmbuck)

    yea…that is all way beyond by knowledge level.

    so, I updated per your edits and now have this:

    add_shortcode( 'widget_exif' , 'exif_outside_loop' );
    function exif_outside_loop() {
    	global $wp_query;
    	$post_ID = $wp_query->post->ID;
    	$img = get_post_thumbnail_id($post_ID);
        $exif ==''; 
     if (function_exists ('exifography_display_exif')) {
        $exif = exifography_display_exif($img);
        if ( !empty($exif) ) $exif = '<h3>Exif title</h3>' . $exif;
      }
    return $exif;    
    }
    
    add_filter('widget_text', 'do_shortcode');

    the good news is that the widget disappears when there is no EXIF data, but the title doesn’t appear when there is. Any thoughts?

    I see a typo I made, but that wouldn’t cause no title. And you were supposed to put the title you wanted, not just my “Exif title” placeholder. You can make sure to use the correct heading type to match your other headings.
    The typo is an extra = in the line $exif =='';

    Be sure to use your browser View Source command to see the actual HTML for shortcode, so you can see if the title is there or if it’s invalid markup or something.

    Thread Starter Josh

    (@joshmbuck)

    The actual title doesn’t really matter until I get it to work ?? but I did try only one = and that still didn’t fix the problem.

    Thread Starter Josh

    (@joshmbuck)

    I just checked the source code… no reference to the title and as far as i can tell no errors on this topic. I did find this, but not sure it’s apropos:

    Uncaught (in promise) Error: You must pass a userId or anonymousId key
    at Object.t.default (content.bundle.js:6)
    at t.identify (content.bundle.js:6)
    at content.bundle.js:1

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Hiding empty widget’ is closed to new replies.