Wordpress action "wp": Cannot modify header information - wordpress

I would want to show some HTML code in my WordPress page. So in my child theme functions.php, I have written an action like this one:
add_action('wp', function() {
if(is_admin()) {
return;
}
$html = '<div class="home_immodvisor">';
echo $html;
});
However, a warning is displayed when I go to the back-office (admin) login page:
Warning: Cannot modify header information - headers already sent by
(output started at
/var/www/html/wp-content/themes/hello-elementor-child/functions.php:88)
in /var/www/html/wp-includes/pluggable.php on line 1296
Warning: Cannot modify header information - headers already sent by
(output started at
/var/www/html/wp-content/themes/hello-elementor-child/functions.php:88)
in /var/www/html/wp-includes/pluggable.php on line 1299
My question is: is it the good action to hook? Or should I use another one than wp?

From my understanding wordpress sends headers after wp so this is the wrong place, since you cannot echo before.

There is no standardized hook/action for inserting a html in a theme. Every theme could handle this differently, some might not even have a hook.
But if you must the wp_head hook will work, but it will not be valid HTML.
The better thing todo is check the documentation of your thema. Or make a child-theme and adjust the tempaltes) you need

Warning: Cannot modify header information - headers already sent by
To solve this add the below code in theme function.php file
ob_start();
It will remove your error.

Related

Triggering the Advanced Custom Fields (ACF) 'acf/save_post' Action

I've read the ACF 'acf/save_post' documentation here: acf-save_postDocs, which states this action enables one to add additional functionally before or after saving/updating a post. I want to test when this action is triggered.
I created a custom post type and a collection of ACF custom fields that are linked to this custom post type.This includes some ACF custom field types that I created using the ACF Starter Kit (see: creating-a-new-field-typeDocs), and they are used for some of the ACF custom fields. When I edit a custom post and then click the 'Update' button in the Gutenberg editor, I expect the 'acf/save_post' action to fire, but it does not seem to do so. Perhaps I am missing something. Here is the relevant code from my theme's 'function.php' file.
function my_acf_save_post($post_id) {
console_log("Testing...");
}
add_action('acf/save_post', 'my_acf_save_post', 20);
function console_log($output, $with_script_tags = true) {
$js_code = 'console.log(' . json_encode($output, JSON_HEX_TAG) . ')';
if($with_script_tags) {
$js_code = '<script>' . $js_code . '</script>';
}
echo $js_code;
}
Citation for the 'console_log' function: Kim Sia (see: https://stackify.com/how-to-log-to-console-in-php/).
I have my web browser's inspector window opened for my custom post type post's editor. After modifying the post and clicking 'Update', nothing displays in the inspector's console pane. I posted about this in the ACF forum but have received no response.
Your feedback is appreciated. Thank you.
Find below the instructions of how to debug that ACF hook in WordPress.
Official document for further details: https://wordpress.org/support/article/debugging-in-wordpress/
Note that there are other, more sophisticated and efficient ways of debugging in WordPress, for example using a proper debugger like xdebug.
These are usually harder to setup though, so here I am giving you the "WordPress in-built" way that might be simple, but is easy and effective.
ENABLE DEBUGGING
First, you need to enable debugging in wp-config.php, which I think you already did, but for others reading this answer, what you should do is:
Open in a text editor the file called wp-config.php, located in the wordpress installation root
Add the following to this file:
define('WP_DEBUG', true); => This enables debugging
define('WP_DEBUG_DISPLAY', false); => This prevents error logging from displaying on screen (affects both frontend and backend)
define('WP_DEBUG_LOG', true); => This logs the output to a file where you can then check it
You must add these yes or yes above the line
/* That's all, stop editing! Happy blogging. */
Done! Now you are logging, by default, to a file that will get generated in the root of the wp-content directory named debug.log.
WARNING! It is recommended to not leave debugging enabled on Production environments, as well as it is recommended to not leave the debug.log file as it could leave sensible site information exposed. Use for development only, and disable and remove file when finished. File can also be saved to a custom file name and location, see docs.
ERROR LOG
console.log is usually used for debugging js.
To log your function in PHP using the enabled debugger above, do the following:
function my_acf_save_post($post_id) {
error_log( 'Saved post ID : ' . $post_id, false);
error_log( var_export($_POST, true), false );
//If you want to log what got saved to the database
error_log( var_export(get_post($post_id), true), false );
}
add_action('acf/save_post', 'my_acf_save_post', 20);
You should find the saved post data and the ACF data recorded in a file named debug.log in your wp-content directory root.
LOGGING TO SCREEN
If instead what you want is to see the output that would have got written to the file, but directly on the screen, you don't need error_log() function, and instead you do that as shown below:
function my_acf_save_post($post_id) {
echo 'Saved post ID : ' . $post_id;
// Add <pre> tags to format the output in an easily readable way:
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
add_action('acf/save_post', 'my_acf_save_post', 20);

wp_redirect not working in custom plugin

I am trying to create custom plugin in wordpress.
We want to create a case like if user is not logged in to the system then user should be redirected login page. I tried wp_redirect and wp_safe_redirect but it is not working. here is my code.
if (isset($_SESSION['game_login'])) {
//Do Something
}else{
wp_redirect('login');
exit():
}
I am getting this warning
Cannot modify header information - headers already sent by (output started at wp-includes/class.wp-styles.php:225) in wp-includes/pluggable.php on line 1216
can someone suggest me in this scenario?
You shouldn't just start output buffers wherever unless you're specifically delaying the final output, such as modifying content on the template_redirect, using add_shortcode, or any numerous scenarios where you intend to buffer the output.
The code in your example should be encapsulated in a function and hooked to one of WordPress' many Action Hooks. Typically this kind of function is added on the plugins_loaded or init hooks.
add_action( 'init', 'redirect_anonymous_users' );
function redirect_anonymous_users(){
if( isset( $_SESSION['game_login'] ) ){
// Do Something
} else {
wp_redirect('login');
exit();
}
}
There may be several reasons causing this issue.
Try this points and hope this may have a fix
Remove blank space from files that are showing error after php ?> tag at end of file, But in your case
it is from core file, So don't modify anything in terms of code just try to remove blank space at the
ending of those files. Also remove blank space from bottom of files
wp-config.php and functions.php
If the above point does not work add this code to your wp-config.php file
ob_start();
error_reporting(0);

Using external file with WP_USE_THEMES set to false causing 404

I know there's a dedicated WordPress StackExchange, however, I'm getting no traction over there.
I have a file in my WordPress template I want to use for a Twitter authentication callback named oauth.php that I'd like to be accessible through htp://mydomain.com/oauth.php:
<?php
define('WP_USE_THEMES', false);
echo "test";
However the file is throwing up a 404??
This actually a issue I'd like a solution for away from just this instance.
EDIT
Using the full file path works as intended, so:
http://example.com/wp-content/themes/mytheme/oauth.php
Is this bad practise?
You could try something like this in your oauth.php:
<?php
//Instead of setting to false, try comment it out
//define('WP_USE_THEMES', true);
/** Load the WordPress Environment */
require('./wp-blog-header.php');
// Fake a 200 OK status header
status_header(200);
//The rest of your code here
?>
This should allow you to access this file at http://example.com/oauth.php and run the code inside leveraging Wordpress functionality but without your template.
NOTE: This may depend on your wordpress version.
Hope it helps!

WP error when setting cookie

I get this error when trying to set a cookie on a WP page.
Warning: Cannot modify header information - headers already sent by(...)
Any help here? It worked fine when I did the php outside of wordpress, but when I move it in, it broke. Nothing special about the cookie(very basic).
So how do I get the cookie to set?
You need to set the cookie before any output is sent to the browser. In your theme's functions.php file, add the following hook:
add_action('init', 'my_cookies');
function my_cookies(){
// set cookie here
}
The init hook runs before your template files (header.php, etc) are included. If you try to put your cookie code inside index.php or single.php or whatever template file, you would have to make sure it goes BEFORE get_header(). Once get_header() runs, you've already sent data to the browser, and you cannot modify the headers (which is necessary to modify cookies).
Its possible you are leaking output somewhere else in your code, before the cookie code. If you even echo a space: echo " " somewhere before your cookie code, you'll get that error. If you have a closing php tag ?> with a space after it in one of your files, this gets sent to the browser, and you'll get that error.

Possible to add a read more link in an RSS Feed?

This may be a weird or stupid question, but I have the following code (http://pastebin.com/PTFtqkvs) and I want to place a simple "read more" link after the description which links to the the article in the rSS feed - however whatever I do isn't working. Is it even possible to add this option and still conform to the rSS guidelines? This is built using a WP system to show Posts in a certain category.
Any help would be greatly appreciated.
You can hook onto feed specific hooks to add that to your feed content. Something like this in your theme's functions.php would work:
function my_super_awesome_feed_linker( $content ){
$extra = "<a href='" . get_permalink() . "'>Read More...</a>";
return $content . $extra;
}
add_filter( 'the_excerpt_rss', 'my_super_awesome_feed_linker' );
This will add a 'read more' link to all your feeds, though.
In order for this to work, you need to use a normal WordPress loop and the function the_excerpt_rss() instead of what you do in your code, echo $post->post_excerpt;. I've modified your pastebin here:
http://pastebin.com/6Y8pewhW
Also, just a word of advice, this won't really work as a template. WordPress has already sent headers by the time you've gotten to the page's template file. So you'll need to find a way to get those headers sent correctly, or to override them. The two easiest ways would be to filter the header content or to query the posts at 'wp_loaded' before headers are sent.

Resources