Skip Navigation

[Résolu] Using wp_enqueue_script for post edit content templates

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
Using wp_enqueue_script for post edit content templates

Solution:
You should try to use the hook "wp_enqueue_scripts" to add the custom js file conditionally.

You can find the proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/using-wp_enqueue_script-for-post-edit-content-templates/#post-1240794

Relevant Documentation:
=> https://stackoverflow.com/questions/20138797/how-to-load-certain-scripts-only-on-single-pages-of-custom-post-type-in-wordpres

This support ticket is created Il y a 5 années et 7 mois. There's a good chance that you are reading advice that it now obsolete.

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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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)

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par julieP Il y a 5 années et 7 mois.

Assisté par: Minesh.

Auteur
Publications
#1240692

I'm using wp_enqueue_script to selectively load .js files. When conditions are used to enqueue for specific pages or post, is_page or is_single are working fine but I'm struggling to implement a condition for post edit content templates.

I've tried

if ( is_single('view-template') )
if ( is_single('my-custom-post-type') )
if ( is_single('123') )
if ( is_single('cred-form') )

but none of them work. The script loads if I remove the condition which confirms the rest of the snippet is OK.

How do we do this please?

#1240693

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - as I understand, you want to apply conditions and post edit screen in admin?

Views offers hook using which you can force the content template dynamically:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template
(But I think you are not looking for this)

If you want to target the views template, views template is stored as postmeta in postmeta table with key "_views_template" which holds the content template ID as meta value.

So, if you want to check for example for post ID 9999 what content template ID is assigned. you should fetch the content template ID as:

$post_id = 9999;
$views_ct_id = get_post_meta($post_id,'_views_template',true);
If($views_ct_id===XXX){
    // do your things here
}
#1240741

Hi Minesh

I'm sorry but I don't understand how I would use this to enqueue a .js file. If I want my .js file to be loaded for a specific custom post type, I would write this:-

if ( is_single('my-custom-post-type') ) {
   wp_enqueue_script( 'my-script', plugin_dir_url( __FILE__ ).'my-script.js', array( 'jquery' ), '', true );
}

so if it needs to load for a content template, I would expect to replace 'my-custom-post-type' with 'view-template' because that is the "post type" from the posts table (not postmeta) but that doesn't work.

#1240794

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Well - if you want to check for content template assigned to single post type.

global $post;
$views_ct_id = get_post_meta($post->ID,'_views_template',true);
If(if ( is_single('my-custom-post-type') and $views_ct_id==XXX) { 
 wp_enqueue_script( 'my-script', plugin_dir_url( __FILE__ ).'my-script.js', array( 'jquery' ), '', true );
}

Where:
- XXX is the value of content template you want to check against that is assigned to post.

But please note that you should use the hook: wp_enqueue_scripts

Please check the following links that may help you:
=> https://stackoverflow.com/questions/20138797/how-to-load-certain-scripts-only-on-single-pages-of-custom-post-type-in-wordpres
=> lien caché

#1240832

My issue is resolved now. Thank you!