Detecting current page from class method
https://wordpress.stackexchange.com/a/199997/86773 As the advice suggested here, switching my hook to “wp” instead of “init” allowed me to make the page comparison I wanted using is_page().
https://wordpress.stackexchange.com/a/199997/86773 As the advice suggested here, switching my hook to “wp” instead of “init” allowed me to make the page comparison I wanted using is_page().
You can use the global variable $submenu. Example to list the child menu of parent post menu:- function admin_init_callback() { global $submenu; $sumenu_list = $submenu[‘edit.php’]; var_dump($sumenu_list); //array of submenu } add_action(‘admin_init’, ‘admin_init_callback’, 999); Profi660 EDIT: Not needed to be used with admin_init hook. I used this in my plugin page and worked very well.
You can examine wp-includes/vars.php for specific logic which implements sniffing for these globals in WordPress. In a nutshell the checks are very basic, especially considering user agent sniffing is inherently a mess. If you need a more reliable implementation you would have to get one elsewhere (or code it yourself). That or use different technique … Read more
You can use wp_get_current_user() instead of using the $current_user global. $my_current_user = wp_get_current_user(); if ( $my_current_user->display_name ) { echo ‘<p>Welcome ‘ . $my_current_user->display_name . ‘</p>’; } Note that I used $my_current_user instead of $current_user as a variable name, because you can run into trouble if you use the global variable’s name.
get_extended() saved me! The returned array has ‘main’ and ‘extended’ keys. Main has the text before the <!–more–>. The ‘extended’ key has the content after the <!–more–> comment. $content = get_post_field( ‘post_content’, get_the_ID() ); $content_parts = get_extended( $content ); echo esc_html( strip_tags( $content_parts[‘main’] ) );
Template tags like the_content() and the_title() are dependent on the global $post variable ($GLOBALS[‘post’]). See the source code for get_post(), which get_the_title(), for example, uses to get the current post so it can get its title: function get_post( $post = null, $output = OBJECT, $filter=”raw” ) { if ( empty( $post ) && isset( $GLOBALS[‘post’] … Read more
Translation works with global variables, you just need to wait until the proper hook to call the translation functions. I haven’t looked at the code or documentation, but just by experimenting, the translations are loaded sometime before the after_setup_theme hook, but after the other hooks that fire before that. That means that if you try … Read more
Themes and plugins are just files that are loaded alongside all the other WordPress files that contain its functions, and those are all loaded whenever WordPress is run. So there’s no special trick or anything weird going on. WordPress just loads the files that contain these functions before it loads themes and plugins. Keep in … Read more
TLDR: Use a cookie Why Not PHP Sessions? PHP sessions don’t work on most hosts ( e.g. WP Engine which say in their docs they do not support PHP sessions ). PHP sessions are incompatible with caching CDNs, caching proxies such as Varnish, and caching plugins. They rely on a session ID stored in a … Read more
filter_input is the proper way to go. If it doesn’t return anything valid, it will return null: $myvar = filter_input( INPUT_POST, ‘something’, FILTER_SANITIZE_STRING ); if ( empty( $myvar ) ) { // Do whatever you would have done for ! isset( $_POST[‘something’] ) } // Use $myvar filter_input won’t throw any notices if the requested … Read more