Results 1 to 9 of 9

Thread: Adding a filter for only specific pages

  1. #1
    dashifen's Avatar
    dashifen is offline Hello World
    Join Date
    Apr 2010
    Location
    Illinois, USA
    Posts
    15

    Default Adding a filter for only specific pages

    Greetings,

    I'm working on a plugin that adds some shortcodes. When those shortcodes are present in the content, I'd like to change the way the title is printed for the page or post. The way I'm handling it now is to add a filter on both the_title and wp_title. Then, I get the content of the post/page from within the filter's function and manually look for the shortcodes that within that content. If I find them, I finally change the way the title looks and if I don't, then I do nothing.

    It's working successfully but it seems like an awful lot of work, but I suspect it might be necessary since the title is printed before the content is handled which is where the system first encounters the shortcodes.

    Is there an easier way?

    Thanks,
    Dash
    David Dashifen Kees

  2. #2
    dashifen's Avatar
    dashifen is offline Hello World
    Join Date
    Apr 2010
    Location
    Illinois, USA
    Posts
    15

    Default

    An update: doing it the way I described above using the code below, means that the titles get changed in the site's menu when you're on one of the plugin pages, too. The menu is printed using wp_list_pages(). Here's the code I'm trying to use:

    PHP Code:

    add_filter
    ("the_title""title_filter");
    function 
    title_filter($title) {
      global 
    $post;
      if(
    strpos($post->post_content"[XYZ]")===false) return $title;
      return 
    "Whee!";

    The shortcode [XYZ] is replaced with a few shortcodes and, eventually, I think I'll switch it from calls to strpos() to a regular expression so that it's not too repetative, but I'm trying to keep the PHP simple for the moment.

    The altered title wouldn't be "Whee!" either, that's just for the purposes of this example.
    Last edited by dashifen; 05-28-2010 at 03:52 PM. Reason: Listed the wrong function name.
    David Dashifen Kees

  3. #3
    dashifen's Avatar
    dashifen is offline Hello World
    Join Date
    Apr 2010
    Location
    Illinois, USA
    Posts
    15

    Default

    Update #2: I've fixed the menu problem after stumbling over the in_the_loop() method. The above PHP has been altered to use this conditional:

    PHP Code:
    if(!in_the_loop() || preg_match(...)) return $title 
    That makes things work out pretty well.
    David Dashifen Kees

  4. #4
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,801

    Default

    Tricky problem. I recall their being another thread around here on this topic but I can't recall where it is in the forum. Maybe someone else will recall.

  5. #5
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    865

    Default

    The basic problem is that you're using shortcodes in the wrong way. They were designed to be in the content and to be replaced by something, not to trigger other actions elsewhere on the page.

    You might look for another way to detect when you need to adjust the title, instead of relying on the user to insert a shortcode onto the post.

  6. #6
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,801

    Default

    Quote Originally Posted by Otto View Post
    The basic problem is that you're using shortcodes in the wrong way. They were designed to be in the content and to be replaced by something, not to trigger other actions elsewhere on the page.
    I can see where you are coming from, but I can also see how having shortcodes load stuff outside of the_content() is useful.

    Something like Contact Form 7 springs to mind. It would make sense for it to only load it's JS and CSS files on pages in which it's shortcode is used, but instead it just loads them across the entire site. The only alternative I've come up with myself is to specify separately where you want the CSS and JS to load, but that's not exactly the most elegant way to do it as it requires two actions to achieve it whereas if it could be done via the shortcode then it would only take one.


    I wish I could remember where I'd read how to do this. It's definitely possible, albeit my recollection is that it was rather awkward way of achieving something so trivial.

    EDIT: My memory banks are coming back a little now. There were a bunch of methods posted, one required parsing the_content() twice to detect the shortcode, the other didn't (and hence was much more efficient) - or maybe it did parse it twice but did it in a more efficient way??? Damn fuzzy memory :(

  7. #7
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    865

    Default

    Quote Originally Posted by Ryan View Post
    Something like Contact Form 7 springs to mind. It would make sense for it to only load it's JS and CSS files on pages in which it's shortcode is used, but instead it just loads them across the entire site.
    I've never understood why people want to bother with this. It doesn't save you any time to not load the JS and CSS, even if they're not being used. Not really. Browser caching eliminates the differences here.

    And if you're one of those people who use trickery to combine CSS and or JS dynamically into a single request, then not loading things is actually actively detrimental to the speed of your site, since everything else now has to load twice.

    If you really, really want to do it, just examine the $post->post_content yourself for the shortcode, then have your shortcode processing function do nothing but return an empty string to eliminate the shortcode from displaying.

  8. #8
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,801

    Default

    Caching does not work when someone visits your site for the first time though. Having 20 something plugins all loading their own CSS and JS files is a real killer for load time (not that I have that many loading, but some people do).

    What I actually intend to do eventually on my own site is to just copy and paste all the CSS and JS I need into single files, compress the hell out of them and serve them all as a single file. I wouldn't do it dynamically for the reason you mentioned.

    Quote Originally Posted by Otto View Post
    If you really, really want to do it, just examine the $post->post_content yourself for the shortcode, then have your shortcode processing function do nothing but return an empty string to eliminate the shortcode from displaying.
    Er, wouldn't you need to load a loop prior to wp_head() firing for that to work? Or am I misunderstanding something here?

  9. #9
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    865

    Default

    Quote Originally Posted by Ryan View Post
    Er, wouldn't you need to load a loop prior to wp_head() firing for that to work? Or am I misunderstanding something here?
    The OP was talking about modifying the_title(), which would be already in the loop. If you wanted to insert CSS or JS, then you'd need to start the loop, grab the data, then use rewind_posts to roll the loop back. This is simpler than it sounds. I use code like this in SFC:

    PHP Code:
    function sfc_share_meta() {
        
    $excerpt '';
        if (
    is_singular()) {
            
    the_post();
            
    rewind_posts(); 
            
    $excerpt strip_tags(get_the_excerpt());
    ?>
    <meta name="stuff" />
    ... 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •