Skip Navigation

[Resolved] How to call an archive page ID in Code Snippets to apply a hook

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 13 replies, has 3 voices.

Last updated by Minesh 1 year, 11 months ago.

Assisted by: Minesh.

Author
Posts
#2356753

I'm trying to call a toolset archive page I designed within Code Snippets to apply a hook to only that archive page.

This is because I have different hook code to insert on different levels of my directory, ex. state level/city level archive pages are different.

For example, I just need to know how to call the toolset archive page/ID within this code,

add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
if( !is_admin() ) {
if ( 123 === $element_id && 7580 === $template_id ) {
$display = true;
}
}
return $display;
}, 10, 2 );

Where "$template_id" is what I need to know how to call the Toolset archive page, and that "7580" is the archive ID of my Toolset archive.

Thanks !
G

#2356867

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi there

I don't think that's an option, plucking the custom archive ID out of nowhere in the context of your filter callback.

You should probably use a WordPress conditional tag like is_archive or is_post_type_archive, depending on what specifically you aim to target.

See the documentation about conditional tags here: https://developer.wordpress.org/themes/basics/conditional-tags/

#2356869

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Toolset offers hook: wpv_filter_force_wordpress_archive - which you can use to force the archive template on specific conditions.
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_wordpress_archive

Will that work for you, you can switch archive IDs based on the conditions added to wpv_filter_force_wordpress_archive hook.

#2356945

I need to target the template pages themselves to apply the hook to, so I can't do this by post type, or archive type, unless I'm missing something...

This is because its all under one taxonomy/archive, and I have hundreds of sub taxonomy pages and will have multiple hundreds more as I add directories. Format is like, taxonomy ex. Laundromats, laundromats/state/city, so you have laundromats/california/los-angeles, san-diego, florida/miami, etc. Except for all 50 states and cities totaling a few hundred archive pages.

On a previous support ticket someone helped me to set a filter in the custom code section, using the,

add_filter( 'wpv_filter_force_wordpress_archive', 'wpv_filter_force_wordpress_archive_tax_1', 30, 2 );
function wpv_filter_force_wordpress_archive_tax_1( $wpa_assigned, $wpa_loop ) {

to query whether an archive page, had a parent page, ie. city archive, or had no parent page, ie. state archive, and then assign their respective archive ID's, and this worked quite well.

Now I just realized, I was able to take the code out of this and make my snippet work using this,

$wpa_assigned = $target_tax_1_city_archive_id

As such,

add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
if( !is_admin() ) {
if ( 123 === $element_id && $wpa_assigned == $target_tax_1_city_archive_id; {
$display = true;
}
}
return $display;
}, 10, 2 );

Seems to work...

Maybe this will help someone else.

Thanks!
G

#2356949

Sorry guys, I spoke too soon!

It seemed to work! But then it was displaying the hook element on BOTH state and city archive pages.

Then I tried to target state and city separately , as I intend to do, to see if assigning each their own would fix this, but instead now I have 2 hook elements showing up on both the city and state level pages...

Using this code I get the hook for the city template, and the state template, both showing up on both city and state archives,

add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
if(!is_admin()){
if ( 1234 === $element_id && $wpa_assigned == $target_tax_1_city_archive_id ) {
$display = true;
} else if ( 5678=== $element_id && $wpa_assigned == $target_tax_1_state_archive_id ) {
$display = true;
}
}
return $display;
}, 10, 2 );

I'm trying to get the city archive to show the 1234 element and the state archive to show the 5678.

Any thoughts on this ? 🙂

Thanks for the help!

G

#2356957

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

We will require to compare the archive ID and archive ID will be available with the hook: wpv_filter_force_wordpress_archive

Can you please share what is the hook code you are using for the hook: wpv_filter_force_wordpress_archive and how you are setting city and state archive ID using wpv_filter_force_wordpress_archive hook?

#2357339

This is the code that I am using in the custom code section under Toolset settings.

Maybe it is also possible to instruct the hook element ID to show up on these archive pages by adding to this custom code somehow... It would be a clean way to do this.

So the following code I got from you guys, Toolset Support, to get the archive template ID page that I wanted, assigned to the city level, and the state level (like I said, with many hundreds of pages, I couldn't simply select what pages to apply it to under the archive template page builder).

As follows,

<?php
/**
* New custom code snippet (replace this with snippet description).
this assigns the state and city taxonomy term archives to respective archive templates based on whether they have parent term pages
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_filter( 'wpv_filter_force_wordpress_archive', 'wpv_filter_force_wordpress_archive_tax_1', 30, 2 );
function wpv_filter_force_wordpress_archive_tax_1( $wpa_assigned, $wpa_loop ) {

// slug of the target taxonomy
$target_tax_1_slug = 'laundromats';
// ID of the WP Archive created for the state level terms
$target_tax_1_state_archive_id = '1234';
// ID of the WP Archive created for the city level terms
$target_tax_1_city_archive_id = '5678';

// check if the archive is for the target taxonomy term
if ( $wpa_loop == 'view_taxonomy_loop_'.$target_tax_1_slug ) {
// get information about the term whose archive page is being viewed
$term = get_queried_object();
// check if the current term has some parent
if($term->parent > 0) {
// if parent term exists it means it is a city term and assign the archive for the city
$wpa_assigned = $target_tax_1_city_archive_id;
}
else
{
// if no parent term exists it means it is a state term and assign the archive for the state
$wpa_assigned = $target_tax_1_state_archive_id;
}
}

return $wpa_assigned;
}

Hopefully you can see a way to call the element ID that I need to display on each archive page from here? 🙂

Thanks!
G

#2358841

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

It seems you are mixing two hooks here "wpv_filter_force_wordpress_archive" and "generate_hook_element_display" I can not be able to guide you in the right direction until I check on your install whats happening and for that I will require admin access details.

Please share the archive URLs as well as admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2359351

Hi Minesh, I would prefer not to be sharing URLs and admin access when it is in a forum post like this even though it's set to private. If this was a private help chat that would be a bit different, but this website is my only source of income so I prefer to keep complete control.

I think the question I am asking is simpler than you think.

I don't think there is any issue of configurations, this is some simple code which I don't know how to call the toolset archive template.

First of all, we're talking about putting code into only 3 places, towards your question of what's happening...
1 - generatepress hooks (I have been using these to insert JSON-LD code on my other pages, and there is a selection to apply to which pages/archives, HOWEVER my issue is that I need a different JSON-LD code depending if it is a state or city level taxonomy archive as these use different archive templates)

2 - toolset custom code section (this is where the code above is inserted and THIS WORKS and sets my city and state archive pages to their CORRECT TEMPLATES)

3 - code snippets plugin (this I haven't really used but THOUGHT I would need to use it to set the generatepress hooks to fire on the city and state archive pages using that code of my original question, currently it isn't being used at all)

I think now you can see that this is a pretty simple set up....

My OBJECTIVE is just to insert different JSON-LD code on to the state and city archive pages.

You're saying I cant use two hooks on one page and the "wpv_filter_force_wordpress_archive" is somehow also a hook?

If so, then can you just tell me where I can insert my JSON-LD code on the Toolset custom code section where I have it assigning the correct archive pages?

I don't see why it would be complicated or NOT work to perhaps just do my JSON-LD code in an generatepress hook, then call it to fire on the city/state archive template pages in the Toolset custom codes section where I am using "wpv_filter_force_wordpress_archive" to set the correct archive template for the state and city levels. Could you show me the code to fire the generatepress hook within where I set the template ID's for city and state there within the code I posted above please?

Or maybe it's a more simple solution that I am asking, just please tell me how I can stick some JSON-LD code on my archive pages for state and city level taxonomies.

It seems to me I need to do this through a generatepress hook, and probably that I can tell that hook to fire on certain archive template pages either in the Toolset custom codes section or using code snippets.

Thanks again for the help!
G

#2359715

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share screenshot of your both city and state archives?

Also, did you created the content template that holds your JSON-LD data and insert that content template within both city and state archives?

If you created different archives for both state and city then what if you create a content template one for city and one for state and insert that content template to respective archives - will that work?

I've created a test site for you, you can auto-login to site using the following link:
- hidden link

You can create replicate your structure with above site and let me know what template you want to display with what archive.

#2360173

I think my set up is again more simple than you think lol.

I'm not even that sure what you're asking if I created a content template that holds the JSON-LD data.

See the picture attached....

I have my top taxonomy, Laundromats for example, and then state, and then cities.

I guess this means that the states and cities are in fact the SAME ARCHIVE.

For example : /laundromats/arizona/chandler

I need my city and state archive pages to be DIFFERENT.

Therefore I am using that code I pasted above in the Toolset custom code section to assign the correct archive page template dynamically if the taxonomy term has a parent term (ie. city) or no parent term (ie. state).

Does this help explain a bit better?

Now all I'm trying to do, is get a hook to fire on the template page for the state, and another one for the city, to put the JSON-LD data on those pages, which is what I was trying to use this code to do,

add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
if( !is_admin() ) {
if ( 123 === $element_id && 7580 === $template_id ) {
$display = true;
}
}
return $display;
}, 10, 2 );

But like I said, maybe there is a way to simply get that element/hook to display by calling its' element ID in the Toolset custom code section where I assign which template page depending on the taxonomy term having a parent term or not?

Thanks for setting up the test site I appreciate that.

"You can create replicate your structure with above site and let me know what template you want to display with what archive."
- As I've explained I'm already showing the correct template with the city and state level archives, all I want to do is to INSERT JSON-LD code onto those pages.

Look at what I added on the test site in the following places:
- Gyms > now has a taxonomy for laundromats, with state and city levels
- Toolset > WordPress Archives > there is an archive template page for both the city and the state level
- Toolset > Settings > Custom Code > assign-state-city-archive-templates > here is the code to assign the correct archive page for both the city and the state levels
- NOTE : ALL OF THIS IS WORKING ON MY SITE, NOW WHAT I'm TRYING TO DO IS ADD JSON-LD data for the city and state levels
- NOTE: the test site uses GENERATEPRESS like my website, but not GENERATEPRESS PREMIUM, the premium version allows me to create elements/hooks with JSON-LD data and insert it into page headers, only it allows you to select an entire archive (ie. laundromats, and all the state and city levels), it doesn't let you choose to show the element whether a taxonomy page has a parent term or not which is what I need to do to put in different JSON-LD data for city and state archive template pages....

Hopefully this really explains clearly what I'm trying to do, please let me know how to get that Generatepress element/hook on those city and state archive template pages 🙂

Thanks!
G

#2360559

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thank you for sharing all required details and its much clear now.

On my site, the code you were added was not working because you added wrong taxonomy slug as follows:

 $target_tax_1_slug = 'laundromats';

I've changed that to:

 $target_tax_1_slug = 'laundromat';

And now I can see, this is for city and it shows "city archive called:
- hidden link

And when I visit the state archive it does shows "state archive called":
- hidden link

Having said that Toolset works as expected with the filter we added.

Please note that Toolset does not control the header, footer or sidebar, the archive using toolset will have only impact on content (body) area. Toolset does control header or sidebar but its limited to limited theme options and will work only if theme is integrated with Toolset.

The Generatepress theme is integrated with Toolset and here is the official doc about what header/sidebar options you can control it:
=> https://toolset.com/documentation/recommended-themes/using-toolset-with-generatepress-theme/#controlling-generatepress-theme-options-for-templates-and-archives

As you can see with above doc, you can control only those options.

Now, regarding the Generatepress hook you are talking about "generate_hook_element_display", I tried to search but I could not able to locate any related information. As you mentioned that you want to add the JSON-LD data and insert it into page headers using the hook "generate_hook_element_display".

You will honestly have to contact the Generatepress support and ask them about how you can use the above hook "generate_hook_element_display" to add the JSON-LD data and insert it into page headers as they are the right person to answer this query because you want to add the JSON-LD data in to archive header. You should check with them and let me know if you need any help from Toolset for questions belongs to Toolset.

#2361069

Ok well now that we're on the same page, maybe this will make more sense to go over my initial question.

I did inquiry with GeneratePress support initially about this, and there is a way to do it:

"
Yes, that’s correct, you can place your JSON-LD code in a Hook Element, and if you have two codes, one for states and the other for cities, you’ll need to create two Hook Elements as well.

With that said, if you have two Hook Elements, in code snippets, you can just have one add_filter with an if and else if statement, specifically, something like:

add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
if( !is_admin() ) {
if ( 123 === $element_id && 456 === $template_id ) {
$display = true;
} else if ( 321 === $element_id && 654 === $template_id ) {
$display = true;
}
}
return $display;
}, 10, 2 );

You’ll need to replace 123 with the id of the first Hook Element and 321 with the other.

So, you basically need to ask how them to get the template_id in an archive page you’ve created so we can use it in a sense like 456 === $template_id and 654 === $template_id.

This is code which can added through Code Snippets is PHP.
"

So they're telling me to ask Toolset how to get the template_id for a Toolset archive page, which is what I've been trying to ask.

QUESTION FOR TOOLSET SUPPORT: how to get the template_id for a Toolset archive page.

The following resources from Generatepress about hooks are helpful, but have just brought me back to the question, how to get the template_id for a Toolset Archive page template.

hidden link
hidden link

They also had suggested using this following code to check if an archive page had a parent page with a specific ID to achieve the state/city filter as follows, is there a way you could see to modify this code to apply only to my "laundromats" taxonomy and filter purely by if there is a parent page or not? That way we would filter, with parent = city, with no parent = state archive pages, and hopefully achieve the same result.

"
The same logic apply if you use generate_hook_element_display, wherein you could probably check if a category has a parent category with a specific ID:

add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
if(!is_admin()){
$cat = get_queried_object();
if ( 10 === $element_id && in_array( $cat->parent, [1,2,3,4,5] ) ) {
$display = true;
}
}
return $display;
}, 10, 2 );
"

Here is the link to the support page thread with GeneratePress if it's helpful,
hidden link

I noticed also a Toolset support ticket where they're looking at checking Toolset archive template ID's and stuff but I don't understand how I can extract what I need from the code I see here:
https://toolset.com/forums/topic/generateblocks-css-not-working-on-toolset-wordpress-archives-template/

Thanks again for the help, I'm not a developer so trying to use the right language to even ask the questions I'm asking isn't easy!

🙂
G

#2361339

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The hook "wpv_filter_force_wordpress_archive" will run after the template redirect hook, please check the following screenshot that might help you when the hook "wpv_filter_force_wordpress_archive1" is called.
=> https://toolset.com/wp-content/uploads/2014/05/wpv_filter_force_wordpress_archive1.jpg

If you need to call the hook "generate_hook_element_display" on wp_head what if you try to use the following code:

Lets consider, that element ID 111 is for city and 222 is for state.

add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
if( !is_admin() ) {

       $term = get_queried_object();
        // check if the current term has some parent
        if($term->parent > 0 and 111== $element_id ) {  // city condition
           $display = true;
       } else  if($term->parent ==0 and 222== $element_id ) {  // state condition
           $display = true;
        }

}
return $display;
}, 10, 2 );

Replace:
- 111 with your original city element ID
- 222 with your original city element ID

Based on this, what it will do is, based on city and state condition, it will display the element.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.