Conditionally loading JavaScript based on the Advanced Custom Fields in the post

ACF has finegrained filters for fields when they get loaded. add_action( ‘wp_enqueue_scripts’, ‘register_my_scripts’, 5 ); function register_my_scripts() { wp_register_script( ‘my-script’, ‘path-to/my-script.js’, array(), ‘version’, true ); } add_filter(‘acf/load_field/name=my_field_name’, ‘load_field_my_field_name’); function load_field_my_field_name( $field ) { wp_enqueue_script( ‘my-script’ ); return $field; } Normally all scripts should be enqueued in wp_enqueue_scripts hook. So you should make sure your script … Read more

Getting key value of WP_Term object in wordpress

It returns an array because Posts can have multiple categories. You just need to get the item from the array whose name you want, ($queried_object[0] for the first one), then get the value out of that the same way you would any PHP object: $name = $queried_object[0]->name; You should probably include some checks to make … Read more

WordPress Template Engine?

Correct, there is no PHP templating engine built into WordPress. This does however give you the flexibility to use a templating engine such as Twig or Blade (and I have worked on sites using each of those), or even completely headless using the REST API.

Alter query on edit.php

Few days ago I wrote a quick solution using pre_get_posts filter to hide some pages in admin area. Maybe you could use it as a good starting point for whatever you’d like to achieve. if ( is_admin() ) add_filter(‘pre_get_posts’, ‘mau_filter_admin_pages’); function mau_filter_admin_pages($query) { $query->set(‘post__not_in’, array(1,2,3) ); // Do not display posts with IDs 1, 2 … Read more

Facebook Comment Count

Final version of code used: function fb_comment_count($link = ‘link’) { global $post; $url=”https://graph.facebook.com/”; $posturl = get_permalink($post->ID); $url .= $posturl; $filecontent = wp_remote_retrieve_body(wp_remote_get($url, array(‘sslverify’=>false))); $json = json_decode($filecontent); $count = $json->comments; if ($count == 0 || !isset($count)) { $count = 0; } $comments = $count; if ($count == 1) { $comments .= ‘ Comment’; } elseif ($count … Read more

How to include external page to wordpress page?

It is actually very simple. just add the following text in at begining of google_map.php page upload this page to your theme folder. go to dashboard to create a page, when create a page chose page telmplate: google map. that is it, you can read more here <?php /* Template Name: google map */ ?>

How to count the length of a post title?

There are actually two problems: The & is encoded as &amp;, so you have to use html_entity_decode() first. Multi-byte characters need more than one byte, and strlen() will fail with them. Don’t use strlen(). So use something like this: $title = html_entity_decode( get_the_title(), ENT_XML1, ‘UTF-8’ ); $length = mb_strlen( $title, ‘utf-8’ );

How To Make Connection To WordPress Data Base In A Plugin?

I found the answer my self. First open your wp-config.php and check the bottom of file that Is that contain the below code?… if ( !defined(‘ABSPATH’) ) define(‘ABSPATH’, dirname(__FILE__) . “https://wordpress.stackexchange.com/”); If yes then add the below code to make the connection in your plugin PHP files to connect with wp-config.php file that contain Database … Read more