- This topic has 9 replies, 3 voices, and was last updated 4 years, 7 months ago by
Tom.
-
AuthorPosts
-
February 12, 2021 at 9:46 am #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!
February 12, 2021 at 12:50 pm #1656315Leo
StaffCustomer SupportHi 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?
February 12, 2021 at 3:28 pm #1656416Randy
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?
February 13, 2021 at 11:23 am #1657161Tom
Lead DeveloperLead DeveloperSo 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.
February 13, 2021 at 12:14 pm #1657242Randy
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!
February 14, 2021 at 1:20 pm #1658202Tom
Lead DeveloperLead DeveloperUsing
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 thepost_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. filterthe_content
.February 14, 2021 at 2:10 pm #1658248Randy
Thanks Tom,
I did a prototype of usingadd_action('generate_after_entry_header'
with priority 1 to just create the div on the fly with the urlparams and such that I need, usingecho '<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 withinadd_filter('the_content'
?February 15, 2021 at 12:12 pm #1659493Tom
Lead DeveloperLead DeveloperI think the performance difference would be negligible.
add_action()
andadd_filter()
are actually the exact same function.February 15, 2021 at 12:57 pm #1659539Randy
Perfect. Got it working nicely using
add_action('generate_after_entry_header')
Thanks again.
February 16, 2021 at 10:52 am #1660753Tom
Lead DeveloperLead DeveloperNo problem 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.