Wordpress: Access to post from wp_headers - php

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...

Related

Use get_post_type(); outside of the loop in functions.php

I am running a function within functions.php to pre-populate an acf field. I need to set a variable which will change depending on which post type. When I run get_post_type(); it does not work. I have tried many variations such as:
get_post_type($post->ID);
$id = get_queried_object_id();
$this_is_post_type = get_post_type($post->$id);
I have also used this wordpress function to no avail:
function get_post_type( $post = null ) {
$post = get_post( $post );
if ( $post ) {
return $post->post_type;
}
return false;
}
Can anybody help me get the post type and store it within a variable I can use please? I am out of ideas. If I use get_post_type within the template files it works fine, it is only at the global level - outside of the loop - that it doesn't.
Based on your comments, you are in fact inside of a filter so you should be able to access what you need. However, because you are also targeting new things, not all functions will behave as you expect, specifically get_queried_object() and similar, and this is a case where the global $post is probably the best way.
I ran this through a WordPress site that I have locally and it worked as expected. I'll leave the logic of the interior parts up to you.
add_filter(
'acf/load_field/key=field_62b1c53ac1a88',
function ($field) {
global $post;
// This should always be true because you are targeting a very specific
// key, however better safe than sorry.
if ($post instanceof \WP_Post) {
// Your logic here
if ('news' === $post->post_type) {
$field['default_value'] = 3;
$field['disabled'] = true;
}
}
// Always return something, no matter what
return $field;
}
);

WordPress "save_post" call only on save post not on update

I am trying to send post data to third-party services when the post is saved via API. I am using hook add_action( 'save_post', [$this, 'save_post_callback'], 100, 3); but this hook seems to be called in update post as well as post-new.php in admin panel. So to get rid of running this hook in post-new.php, I have checked the $_POST request but I am not able to filter the update post since I want to call API only in save the post, not in an update.
There seems to be the third parameter in callback function $update but it's not working either. Below is my code that needs to be called only in save a post but it's not working. Any help would be greatly appreciated.
function save_post_callback( $post_id, $post, $update ) {
// (!$update) => this doesnot seems to work
if(!empty($_POST) && $post->post_type == "post"){
//run only when save post
}
}
simple way is to check if _wp_http_referer last part is post-new.php or not.
here is a simple code
function save_post_callback($post_id, $post, $update)
{
// (!$update) => this doesnot seems to work
if ( ! empty($_POST) && $post->post_type == "post" ){
$end = explode('/', $_POST[ '_wp_http_referer' ]);
$end = end($end);
if($end == 'post-new.php'){
//echo 'it is new post';exit();
//do what you want here.
}
}
}
From StackExchange here. It looks like a clever way is to compare the post_date & post_modified to determine it is a new post.
$is_new = $post->post_date === $post->post_modified;
if ( $is_new ) {
// first publish
}

Run comments_template() function in Wordpress Plugins

I include the comments on the page afterwards with ajax, required code is in the plugin, but comment_template() function does not work when the request goes plugin file, Ajax request is successful, but there are no comments.
Codes that go to Ajax are as follows.
add_action('wp_ajax_nopriv_comment_update_get','comment_update_get');
add_action('wp_ajax_comment_update_get', 'comment_update_get');
function comment_update_get(){
global $withcomments;
$withcomments = 1;
$post = get_post($_POST['post_id']);
comments_template();
die();
}
From the Code Reference:
Will not display the comments template if not on single post or page,
or if the post does not have comments.
If you take a look at the source code, you'll notice that the comments_template() function checks whether we're currently on a post or a page, or if the global $post object is set up.
So:
function comment_update_get(){
// Set up our required global objects
global $post, $withcomments;
$withcomments = 1;
$post = get_post( $_POST['post_id'] );
// Load the comments template
comments_template();
// We're done here
wp_die();
}
add_action( 'wp_ajax_nopriv_comment_update_get','comment_update_get' );
add_action( 'wp_ajax_comment_update_get', 'comment_update_get' );

Wordpress get post id

I'm creating a plugin OOP for wordpress. The plugin creates a new custom post type called teams. Within a team page a could use the shortcode [program] to generate some predefault html code. Also i've created custom fields with new meta boxes.
The problem however is: when i'm entering the page thats calling the plugin, equal the team page with the sortcode i need to get post id within my plugin to retrieve get_post_meta().
I've tried the following things:
public function __construct(){
// not working
$post;
$post->ID;
// not working
global $wp_query;
$post_id = $wp_query->post->ID;
$post = get_post( $post_id );
// not workiing
echo '<pre>';
print_r('post_id:' . get_the_ID());
echo '</pre>';
}
How could i receive the custom post id within my plugin when i visited the page from frontend (so the plugin is called, runs the shortcode)
My main class gets loaded like this:
function run_plugin() {
$plugin = new MyPlugin();
$plugin->run();
}
run_plugin();
Within MyPlugin the constructor looks like
public function __construct() {
if ( defined( 'PLUGIN_NAME_VERSION' ) ) {
$this->version = PLUGIN_NAME_VERSION;
} else {
$this->version = '1.0.0';
}
$this->plugin_name = 'MyPlugin';
if(!empty(get_option($this->plugin_name))){
$this->clientID = get_option($this->plugin_name)['client_id'];
}
$this->load_dependencies();
$this->set_locale();
$this->define_admin_hooks();
$this->define_public_hooks();
$this->define_shortcodes();
}
If your plugin constructor is getting called too early, the post data won't be set up and ready to use.
You'll need to hook into one of WPs actions that run after everything is ready. The init action should be enough for the post data, but depending on what else you need you can hook into wp_loaded, as it doesn't run until after WordPress is fully loaded.
function run_plugin() {
$plugin = new MyPlugin();
$plugin->run();
}
/* run_plugin(); */ // <- instead of this....
add_action( 'wp_loaded','run_plugin' ); // <- ...do this
Try to define post as global like
global $post

Unable to get page name in wordpress

I am customizing a wordpress plugin which is used for popup email subscribing. when the user submits email he gets a confirmation email in which iam sending my custom html to him. I have five different pages on my web site and every page has that popup. what iam doing is that i want to get the name of my page than according to that i want to send html in email. I did exactly the same thing for other plugin and it worked but in this popup plugin iam unable to get the page name from where it is called.
I have tried following things but failed.
global $post; /* this worked perfect on other plugin */
$pagename = $post->post_name;
if($pagename=="page1")
{
// html page1 //
}
else
{
// html page2 //
}
Just tried this
$slug = basename(get_permalink());
if($slug=="page1") and so on
Here you go, you need to get title for this, as
$post = get_post( $post );
$pagename = isset( $post->post_title ) ? $post->post_title : '';
Hope it helps.
The best way is to use get_queried_object. It retrieve the currently-queried object - page, post, taxonomy, whatever...
You can try this code, its working for me:
$qo = get_queried_object();
if ( 'page' !== $qo->post_type ) {
//Here you can be sure, that you are in page query, so this is available
$pagename = $qo->post_name;
}
Please try to use this:
$pagename = get_query_var('pagename');
if ( !$pagename && $id > 0 ) {
// If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
$post = $wp_query->get_queried_object();
$pagename = $post->post_name;
}

Categories