[Resolved] Change content of an elements based on page

Home Forums Support [Resolved] Change content of an elements based on page

Home Forums Support Change content of an elements based on page

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1656144
    Randy

    Hi!
    Currently, I have a div with a bunch of properties at the top of 20+ pages.

    I change those properties on the fly using add_filter('the_content'
    and return $content slightly modified based on which page it is on.

    The shortcoming is that I have this div manually inserted on 20+ pages so it is difficult to maintain (e.g. if I want to change a property I have to change it in 20+ places). (link shared in private info)

    What I’d rather do is be able to just have the div inserted in ONE hook element, then be able to modify the content of the ELEMENT on the fly similar to how I am modifying the_content

    I have GP Premium and GB Pro, so hoping there is a way to do this.

    Thank you!

    #1656315
    Leo
    Staff
    Customer Support

    Hi Randy,

    Sorry not sure if I fully understand.

    What I’d rather do is be able to just have the div inserted in ONE hook element, then be able to modify the content of the ELEMENT on the fly similar to how I am modifying the_content

    That sounds like exactly what you need to do. What is the issue there?

    #1656416
    Randy

    I put the div into a Hook Element, hooking into generate_before_content on ALL of my static pages.

    But what/how do I use add_filter to MODIFY that Element content from my child theme’s functions.php?

    (If I were modifying just a PAGE, I’d use add_filter(the_content and modify $content before I return it. But I’m not clear how to do that with a Hook Element from functions.php)

    Maybe I’m misunderstanding something basic here?

    #1657161
    Tom
    Lead Developer
    Lead Developer

    So the content of this div changes depending on the page?

    If so, the best thing to do would be to use custom fields.

    For example:

    <div class="my-div">
        <?php
        $custom_field = get_post_meta( get_the_ID(), 'my_custom_field', true );
    
        if ( $custom_field ) {
            echo $custom_field;
        }
        ?>
    </div>

    That will output the page-specific data if it’s been added as a custom field to each page.

    #1657242
    Randy

    Hi Tom

    I looked at this option but I don’t think it will work as I do a lot of stuff on the fly based not only on the page but also things such as getting url query parameters from the current page and appending them into an iframe src embedded within the div.
    So the content in the div is dynamic. (Example link in private info)

    It works ok right now with search replace php code within the_content but I was hoping to do that search replace within one Element rather than across 20+ pages.

    I suppose the other option would be to just CREATE the div on the fly with an add_action hook within generate_before_content, but I was hoping to avoid that.

    Unless you have other ideas?

    Really liking GB Pro by the way!

    #1658202
    Tom
    Lead Developer
    Lead Developer

    Using the_content isn’t a terrible idea – you should be able to add some conditions in there so it only applies to Elements by checking the post_type of the query.

    Otherwise, you can use $_GET in your Hook to get query params if you want to add them directly to the hook vs. filter the_content.

    #1658248
    Randy

    Thanks Tom,
    I did a prototype of using add_action('generate_after_entry_header' with priority 1 to just create the div on the fly with the urlparams and such that I need, using echo '<div id= etc… (see below)

    Seems to work fine. Just quick question…from a performance/speed standpoint, knowing GP’s architecture, is there an advantage/disadvantage of using add_action('generate_after_entry_header' over doing the same within add_filter('the_content' ?

    #1659493
    Tom
    Lead Developer
    Lead Developer

    I think the performance difference would be negligible. add_action() and add_filter() are actually the exact same function.

    #1659539
    Randy

    Perfect. Got it working nicely using add_action('generate_after_entry_header')

    Thanks again.

    #1660753
    Tom
    Lead Developer
    Lead Developer
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.