How do I add some custom PHP to the "post edit"-page, and get the post_id, when the post is published?
Like this red area:
you can use get_current_screen() function (Link to codex) to check that you're on a post edit page and then get post id with a global $post variable.
Example:
add_action('admin_notices', 'screen_info');
function screen_info() {
$screen = get_current_screen();
if(is_admin() && $screen->id == 'post') {
global $post;
$post_id = get_the_ID();
// your code here
}
}
Related
I'm using a theme template and when i try to get post ID it returns the ID of the template not the ID of the actual single post.
the template ID is: 215
the post ID is: 1911
the following code will only output 215
function metavalue() {
global $post;
$meta = get_post_meta($post->ID, 'product_url', true);
return $meta;
}
add_shortcode('url_short', 'metavalue');
get_the_ID(); the_id(); $post->ID; will also output 215. i need a way to get the actual single post ID so i can get the custom field value from 'product_url'.
I've contacted support with the theme authors on this topic as well but for the time being i've found a way to work around it.
function metavalue() {
global $wp;
$url = home_url( $wp->request );
$correct_post_id = url_to_postid( $url );
$meta = get_post_meta($correct_post_id, 'product_url', true);
return $meta;
}
add_shortcode('url_short', 'metavalue');
I'm trying to set a new http Header for my wordpress installation but I'm not able to work with $post object inside my new wp_headers filter function. I want to send different headers for diferent post types and use Go(lang) for caching stuff (home project).
function add_new_header($headers) {
$headers['PostId'] = get_the_ID();
return $headers;
}
add_filter('wp_headers', 'add_new_header');
Seems Like I can't access to Post / get_queried_object_id() in the hook as it's not started.
So, referencing post attributes, you have to do in the "template_redirect" hook. As in that moment the Post exists...
add_action('template_redirect', 'add_new_header');
function add_new_header($headers) {
$post_id = get_queried_object_id();
if( $post_id ) {
header("PostId: " . $post_id) ;
}
}
Hope helps to someone.. someday...
I'm trying to show a list of posts inside a var_dump, this is the actual code:
function deleted_cpt_orders() {
global $post_type, $post;
if ( $post_type == 'cpt_orders' ) {
var_dump($post);
}
}
add_action( 'trashed_post', 'deleted_cpt_orders' );
If i delete only 1 post the var_dump is shown, but if i delete 2 posts or more the result is
NULL
global $post is supposed to hold a single post object. If you delete multiple objects, it won't be set. However, trashed_post hook passes post id, therefore, you can do the following
function deleted_cpt_orders($object_id)
{
$post = get_post($object_id);
if ($post->post_type == 'cpt_orders')
{
var_dump($post);
}
}
add_action('trashed_post', 'deleted_cpt_orders');
In my wordpress theme i have included meta box for Title, description and redirect section. I have a input field for the redirect box under every post and page id. If i add any url to a particular page/post id for that redirect box, the particular page/post id should redirect to my own url given in that redirect box. Is there any function to do this? I have searched in stackies and i get the following code. But its not working.
function my_permalink_redirect($permalink) {
global $post;
if ($post->ID == your_post_id_here) {
$permalink = 'http://new-url.com/pagename';
}
return $permalink;
}
add_filter('get_the_permalink','my_permalink_redirect');
Try:
add_filter( 'the_permalink', 'filter_function_name_7062', 10, 2 );
function filter_function_name_7062( $permalink, $post ){
global $post;
if ($post->ID == 684) {
$permalink = 'http://sample.com/1';
}
if ($post->ID == 444) {
$permalink = 'http://sample.com/2';
}
return $permalink;
}
Try:
function my_permalink_redirect($permalink) {
global $post;
if ($post->ID == your_post_id_here) {
$permalink = 'http://new-url.com/pagename';
wp_redirect("'.$permalink.'", 301);
exit;
}
}
add_filter('get_the_permalink','my_permalink_redirect');
I have the following code to update the post_meta when a post is created. It is very simple,just storing the its own post_id in a meta field(might add more in the future)
The following code is not working, I guess it is because the $post_ID is blank, how do I pass the post_id of newly created post to the function update_postmeta (in function.php)?
//code from function.php
add_action('save_post', 'update_postmeta');
function update_postmeta($post_ID) {
update_post_meta($post_ID, 'related_id',$post_ID);
}
Here is a good boilerplate for you:
function update_postmeta($post_id) {
global $post;
// Post meta isn't sent for autosaves
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
update_post_meta($post->ID, 'related_id', $rand_id);
}
No where in your code is $rand_id defined though.