ID for posts/blogs page

consider to use: $postspage_id = get_option(‘page_for_posts’); and then change the rspective line in your code to: $leftSidebar = get_post_meta( $postspage_id, ‘_my_meta’, true ); from: http://www.blog.highub.com/cms/wordpress/wordpress-front-page-posts-page-id/

Can’t get post ID in functions.php?

The post ID is available after the query has been fired. The first hook that is safe to get post id is ‘template_redirect’. If you can modify your function to accept a post id as argument, like so: function em_change_form($id){ $reg_type = filter_input(INPUT_GET, ‘reg_typ’, FILTER_SANITIZE_STRING); if($reg_type === ‘vln’){ update_post_meta($id,’custom_booking_form’, 2); } elseif ($reg_type == ‘rsvp’) … Read more

Get the current post ID as a variable in Javascript

You can pass variables to javascript using wp_localize_script function: https://codex.wordpress.org/Function_Reference/wp_localize_script Add the following to functions.php if(!function_exists(‘load_my_script’)){ function load_my_script() { global $post; $deps = array(‘jquery’); $version= ‘1.0’; $in_footer = true; wp_enqueue_script(‘my-script’, get_stylesheet_directory_uri() . ‘/js/my-script.js’, $deps, $version, $in_footer); wp_localize_script(‘my-script’, ‘my_script_vars’, array( ‘postID’ => $post->ID ) ); } } add_action(‘wp_enqueue_scripts’, ‘load_my_script’); And your js file (theme-name/js/my-script.js): jQuery(document).ready(function($) { … Read more

Settings API – creating reusable form elements?

You’re absolutely right that you can pass reusable form field markup to add_settings_field(). The trick is to define the data type for each setting, and then pass the same callback to each call to add_settings_field(). Within that callback, you simply add a switch that includes cases for each data type. Here’s how I do it … Read more

What is an alternative to get_page_by_title()?

If you don’t force a title, and obviously do not know the id, the only way you can do it is by letting the user select which page to use, usually done in the theme’s options page, but can be done in the costumizer, or even as part of page editing

Creating a WordPress admin page without a menu for a plugin

I am less convinced that I know what you are doing than I once was. // Add menu and pages to WordPress admin area add_action(‘admin_menu’, ‘myplugin_create_top_level_menu’); function myplugin_create_top_level_menu() { // This is the menu on the side add_menu_page( ‘MyPlugin’, ‘MyPlugin’, ‘manage_options’, ‘myplugin-top-level-page’ ); // This is the first page that is displayed when the menu … Read more

Get previously visited page ID

Break this down into two parts: First, we create a variable that stores that last-visited page URL, like this: $prev_url = isset($_SERVER[‘HTTP_REFERER’]) ? $_SERVER[‘HTTP_REFERER’] : ”; Then, you could either use substr and strpos to trim down everything between ?= and the / after the ID number. like this: $prev_url=”http://www.yoursite.com/?p=123″; $id_block = substr($prev_url, strpos($prev_url, “?p=”)+1); … Read more

How to obtain the user ID of the current profile being edited in WP-Admin?

There is a global variable called … $user_id available on that page. Always. From user-edit.php: $user_id = (int) $user_id; $current_user = wp_get_current_user(); if ( ! defined( ‘IS_PROFILE_PAGE’ ) ) define( ‘IS_PROFILE_PAGE’, ( $user_id == $current_user->ID ) ); if ( ! $user_id && IS_PROFILE_PAGE ) $user_id = $current_user->ID; elseif ( ! $user_id && ! IS_PROFILE_PAGE ) … Read more

get all posts ID from a category

The thing to remember about get_posts is that is uses a WP_Query object internally. get_posts source: <?php /** * Retrieve list of latest posts or posts matching criteria. * * The defaults are as follows: * ‘numberposts’ – Default is 5. Total number of posts to retrieve. * ‘offset’ – Default is 0. See {@link … Read more