Support » Plugin: Secondary Title » Auto show secondary titles not working with some titles

  • Resolved Manuel Sailer

    (@msailer)


    Hello,

    there seems to be a problem when adding secondary titles to posts with (some?) special characters in the title. The secondary title won’t get auto shown.
    For me the problem occurred in a post with the title Mai & Juni 2021.

    By adding some debug output to function secondary_title_auto_show in hooks.php I identified the validate secondary title check as the problem’s cause.
    This is my debug output:

    $title: Mai & #038 ; Juni 2021 [without spaces in the ampersand code]
    wptexturize( $post->post_title ): Mai & Juni 2021
    $title !== wptexturize( $post->post_title ): 1

    In $title the ampersand is encoded while wptexturize(...) returns it non encoded. The comparison is true and $standard_title is returned instead of applying the secondary title.

    For fixing my problem I added passing $title through htmlspecialchars_decode(). This converts & #038 ; (without spaces) back to &.
    But will it work for the titles you had in mind when adding this condition?

    Maybe you could take my fix for the next version of your plugin or find a better way to make work auto showing of secondary titles in posts with special characters in the title.`

Viewing 15 replies - 1 through 15 (of 24 total)
  • Plugin Author thaikolja

    (@thaikolja)

    Hi @msailer,

    Thanks a lot for your suggestion. Encoding is – unfortunately – always tricky. Your solution sounds like worth looking at. I’ll check it out in the near future.

    So that you don’t lose your custom code changes when a new update is pushed without the amendments you’ve made in this post, don’t forget there are filters that you can run in your functions.php or wherever.

    Plugin Author thaikolja

    (@thaikolja)

    Hi again,

    Please excuse my dumbness, but I can’t reproduce the error. Could you give me a step-by-step guide on how I can generate the bug? Thanks so much!

    Thread Starter Manuel Sailer

    (@msailer)

    Hi @thaikolja ,

    thank you very much for your quick reply.
    But don’t be so hard on yourself. The dumbness might be on the other side. 😉

    To give you a working step-by-step guide I did some more investigation and found out that the problem only occurs when the output is processed by the wp-Typography plugin.
    Without having wp-Typography installed the problem does not occur but can be easily switched on/off by enabling/disabling this plugin.

    So this is the step-by-step guide to reproduce the error:

    1. Install wp-Typography plugin and enable it.
    2. Create a new post with an & in the title (e. g. Main & Title) and any Secondary Title and publish it.
    3. Watch the post in your browser.
      Secondary Title should not be visible.
    4. Disable wp-Typography plugin and reload the post in the browser.
      Secondary Title should be visible.

    Of course I would be happy if Secondary Title and wp-Typography could be used together. But if you tell me that I have to solve this compatibility problem myself, I will accept that and apologize for blaming your plugin and stealing your time.

    Have a nice evening.

    Plugin Author thaikolja

    (@thaikolja)

    Hi @msailer,

    Not at all, that’s what this community is for! Let me have a look and I’ll get back to you in a short while with a – hopefully working – fix.

    Plugin Author thaikolja

    (@thaikolja)

    Hi again, @msailer,

    Please check the latest dev version. It should support the WP Typography plugin, based on the few tests I’ve made. Feedback would be very much appreciated!

    Thread Starter Manuel Sailer

    (@msailer)

    Hi @thaikolja ,

    I tested the dev version you provided and had to make some little changes to make it work. This code is working for me:

    /** Validate secondary title */
    if ( ! $secondary_title || get_option( "secondary_title_auto_show" ) === "off" || is_admin() ) {
      if ( empty($secondary_title) ) {
        return $standard_title;
      }
    
      $secondary_title = wptexturize( $secondary_title );
    
      if ( class_exists( "WP_Typography" ) ) {
        $secondary_title = htmlspecialchars_decode( $secondary_title );
      }
    }

    The check for empty secondary title is necessary as non filled secondary titles on posts are stored as empty _secondary_title entries in the postmeta database table.
    In the dev version secondary_title_auto_show generates HTML markup for these also. So content added by CSS :before and :after the empty secondary title gets rendered. Returning $standard_title in this case (as in the previous official version) solves the problem.

    And of course secondary titles work on posts with & in the title and wp-Typography enabled. Thank you very much! 🙂

    Plugin Author thaikolja

    (@thaikolja)

    You’re great, @msailer, thanks for all your help. I’ve implemented your change, you can download it here. And here you can see the compared versions if you’re interested.

    My brief tests worked fine. I hope yours do, too.

    If you’re somewhat happy with Secondary Title, a review is always highly appreciated 🙂 Thanks again for your coding support.

    Thread Starter Manuel Sailer

    (@msailer)

    Hi again @thaikolja ,

    my test cases also work if I reduce the validation to

    /** Validate secondary title */
    if ( ! $secondary_title || get_option( "secondary_title_auto_show" ) === "off" || is_admin() ) {
      if ( empty($secondary_title) ) {
        return $standard_title;
      }
    
      $secondary_title = wptexturize( $secondary_title );
    }

    The important thing was that you removed this comparison: $title !== wptexturize( $post->post_title )`. If there is no such condition there is no need for special handling when wp-Typography is active.

    Plugin Author thaikolja

    (@thaikolja)

    Thanks, but I’d like to keep it as is. Numerous users have it installed and active, and the less I change important code and validations, the less the chance anyone experiences bugs – there’s just so many plugins out there that might interfere with Secondary Title’s output. But I do appreciate your input. If you have any more suggestions, I’m all ears!

    Hi @msailer, @thaikolja, wp-Typography author here. When you enable wp-Typpography, wptexturize will be non-functional (because it’s function is basically a less sophisticated version of what wp-Typography does).

    The reason that “auto show” is not working is that the condition $title !== wptexturize( $post->post_title ) is true. I’m not sure why you are checking for that to be true, but it only ever works by accident. If you look at default-filters.php, besides wptexturize, other filters are also run even on a pristine WordPress site without any other plugins, with a priority low enough to be earlier than secondary_title_auto_show: convert_chars, trim.

    You can easily test this by adding a space at the start of a post_title. You’ll see that the secondary title won’t show anymore for this post (due to the trim filter).

    • This reply was modified 1 year, 2 months ago by pepe. Reason: example added
    Plugin Author thaikolja

    (@thaikolja)

    Hi @pputzer,

    Thanks for joining in.

    So, wptexturize() can be safely removed when using wp-Typography?

    Thread Starter Manuel Sailer

    (@msailer)

    The reduction I suggested does not change important code and validations.
    It leaves the conditions as you already changed them but removes code that you added as special handling for wp-Typography. This code is not required as there is no condition anymore that compares the outputs of htmlspecialchars_decode and wptexturize.

    Plugin Author thaikolja

    (@thaikolja)

    Okay, so this code block will do it, am I right?

    	/** Validate secondary title */
    	if ( ! $secondary_title || get_option( "secondary_title_auto_show" ) === "off" || is_admin() ) {
    		if ( empty( $secondary_title ) ) {
    			return $standard_title;
    		}
    
    		$secondary_title = wptexturize( $secondary_title );
    	}

    That’s exactly the code @msailer posted as a working version above.

    Sorry, I lost a bit track here.

    Thread Starter Manuel Sailer

    (@msailer)

    You are absolutely right! 🙂

    @thaikolja: When wp-Typography is active, wptexturize is short-circuited by the run_wptexturize filter, so yes, not calling it will not change the output.

    The question is, what was $title !== wptexturize( $post->post_title ) supposed to guard against?

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘Auto show secondary titles not working with some titles’ is closed to new replies.