Execute only on a certain page

The is_page function accepts any of the page ID, the page title or the page slug. It looks like you’re trying to give it an URL fragment instead. You say that ‘contact-us’ is the page slug for your contact page, so try if (is_page(‘contact-us’)) { without the forward slashes, i.e. passing the page slug, instead.

Count Title and Post Characters

You can use this function wp_strip_all_tags. strlen( wp_strip_all_tags($post->post_content)); With that you get rid of all the HTML tags. Just a little extra detail, you might want to strip the shortcodes, too. You can use this function strip_shortcodes You will end up with something like this: strlen( wp_strip_all_tags(strip_shortcodes($post->post_content))); To convert the encoded entities to the corresponding … Read more

Add multiple custom post types in functions.php, but only one custom post type show in dashboard

add add_action(‘init’, ‘create_new_cpt2’); in your snippet and modify add_db_to_article function as below, function add_db_to_article ($post_types) { $custom_post_types = array (‘article’, ‘news’); $output = array_merge($post_types, $custom_post_types); return $output; } Note: ‘news’ is added in $custom_post_types array.

Using a function written in my functions.php file within the header.php file

As discussed in comments you actually just want the URL. Here’s your code modified to be a get_avatar_url hook instead: add_filter(‘get_avatar_url’, ‘lb_acf_profile_avatar_url’, 10, 5); function lb_acf_profile_avatar_url($url, $id_or_email, $args) { $user=””; // Get user by id or email if (is_numeric($id_or_email)) { $id = (int) $id_or_email; $user = get_user_by(‘id’, $id); } elseif (is_object($id_or_email)) { if (!empty($id_or_email->user_id)) { … Read more

Disable REST API for a user ROLE

The plugin has a filter drh_allow_rest_api which determines whether the current user has full access and can skip the whitelist check. By default this is just is_user_logged_in(): /** * Allow carte blanche access for logged-in users (or allow override via filter) * * @return bool */ private function allow_rest_api() { return (bool) apply_filters( ‘dra_allow_rest_api’, is_user_logged_in() … Read more

Enqueue script o style only if a template part is loaded

You can use the get_template_part_{$slug} hook, which fires before a template part is loaded. You can find the reference here, which I find more useful than the official reference. So, I tested it in the twentytwentyone theme (yes I modified the theme directly but only for testing). I tested it with the template-parts/content/content-single.php part, and … Read more

How to add another user to this remove_menu function?

Why not just add another if? function remove_menus(){ global $menu; global $current_user; get_currentuserinfo(); //check first user if($current_user->user_login == ‘user1’){ $restricted = array( __(‘Links’), __(‘Comments’), __(‘Appearance’), __(‘Plugins’), __(‘Profile’), __(‘Tools’), __(‘Settings’) ); end ($menu); while (prev($menu)){ $value = explode(‘ ‘,$menu[key($menu)][0]); if(in_array($value[0] != NULL?$value[0]:”” , $restricted)){unset($menu[key($menu)]);} } } //check second user if($current_user->user_login == ‘user2’){ $restricted = array( __(‘Links’), … Read more

What is the most efficient method for loading widgets in functions.php

Both are acceptable but not recommended. Use locate_template() instead because a child theme can overwrite the loaded file then. Example: $found = locate_template( ‘functions/my-custom-widget.php’, TRUE, TRUE ); The first TRUE tells WordPress not only to search for the file but to load it actually. The second makes it a require_once call. The function return the … Read more