Unable to return values from function

The foreach surrounding your return line means only the first item will be returned – perhaps that’s what you want? If not, then you should assign $value[2] to a local variable (concatenating if you want a string, or an array perhaps?) and the return the local variable after the loop. function get_admin_menus() { global $submenu, … Read more

Call to undefined function is_home() or any conditional tags

It’s a timing issue. This tries to call is_home() before that function exists. $my_ver = check_home(); It works in the function called by add_action because then is_home() does exist because WP has fully loaded. If you load php files in your plugin, then you can call check_home anytime in those files. For example: define( ‘MY_PLUGIN_PATH’, … Read more

current_user_can comma list vs OR (||) list

The current_user_has_role() is not a WordPress core function, on the other hand there’s wc_current_user_has_role( $role ) in WooCommerce where $role is a string, but 3rd party plugins are general off-topic. Creating this function was closed as wontfix in this ticket: https://core.trac.wordpress.org/ticket/22624 where Andrew Nacin summarized it neatly: Roles are merely a grouping of capabilities that … Read more

How to cut off question mark in url with functions.php

Try this, which uses the parse_request hook: (you can add other conditions, e.g. check whether $wp->query_vars[‘name’] is not empty, if you want to) add_action( ‘parse_request’, ‘wpse_400900_parse_request’, 1 ); function wpse_400900_parse_request( $wp ) { // Redirect if the URL ends with a ? like https://example.com? if ( ! empty( $_SERVER[‘REQUEST_URI’] ) && ltrim( $_SERVER[‘REQUEST_URI’], “https://wordpress.stackexchange.com/” ) … Read more

how to get the post ID from wp_insert_post

I’ve just tested your code in a function hooked on wp_body_open and it works: add_action( ‘wp_body_open’, ‘function_test’ ); function function_test() { $tittle=”My new page”; $newPage = array( ‘post_type’ => ‘page’, ‘post_title’ => $tittle, ‘post_status’ => ‘publish’ ); $post_id = wp_insert_post( $newPage); print_r($post_id); } Where do you use this code ?

calling a custom field value inside functions.php

if you’re not planning on adding much html there is no need to use ob_start just return the php: function wpb_description_shortcode( ){ if (get_post_meta(get_the_ID(), ‘song_number’, true)) { return get_post_meta(get_the_ID(), ‘song_number’, true).'<br/>’; } else { return; } } // Register shortcode add_shortcode(‘my_description_number’, ‘wpb_description_shortcode’); I’m not sure what this data is, but it would be a good … Read more

Add custom text automatically on each post

You can use a regular expression to sniff for it on the the_content filter. add_filter( ‘the_content’, ‘wpse403518_custom_text’ ); function wpse403518_custom_text( $content ) { $regex = ‘/^([^\.!?]+)([\.!?])(.*)$/’; $replace=”$1 2022$2$3″; $content = preg_replace( $regex, $replace, $content ); return $content; } The regex The $regex variable is a regular expression, and what it does, roughly, is this: ^ … Read more