solution to load katex conditionally for custom fields
-
Hi Thomas! Thanks for making such an amazing plugin!
It seems one common problem others and I have ran into loading the katex scripts conditionally
currently the plugin checks ONLY posts types for the short code before enqueuing the katex script on that page.. a lot of users would also like to use katex in custom fields as well… but the only way (currently) is to enqueue the katex script globally..in my case, multiple acf fields contained the the [katex][/katex] shortcode.. others might be using custom fields from other plugins like an LMS or quiz platform…
i used a simple fix is to just have the code additionally check any custom feilds for any instances of [katex] shortcode and then enqueue the katex script on that page.
ill post the code that worked for me below and hopfuilly others can use it… and maybe it can included into the plugin natively.
// for checking custom fields function has_katex_shortcode_in_acf_fields($post_id) { // Get all custom fields of the post $custom_fields = get_post_custom($post_id); foreach ($custom_fields as $key => $values) { foreach ($values as $value) { if (strpos($value, '[katex') !== false) { return true; // Found the shortcode } } } return false; }
Enqueuing logic … (note this is just general code soltion.. will need to tweak a bit based on plugins Enqueuing function paramaters, etc.
function enqueue_katex_for_acf_fields() { global $post; if (is_singular('your_custom_post_type') && has_katex_shortcode_in_acf_fields($post->ID)) { // plugins ensuing logic. } } add_action('wp_enqueue_scripts', 'enqueue_katex_for_acf_fields');
Again just to clarify im using this for ACF fields and it worked for me but might be different for others.
- The topic ‘solution to load katex conditionally for custom fields’ is closed to new replies.