Warning: Cannot modify header information - wordpress

Warning: Cannot modify header information - headers already sent by (output started at /home/content/51/5126851/html/wp-includes/post-template.php:54) in /home/content/51/5126851/html/wp-includes/pluggable.php on line 890
I know I need to do something to my post-template.php file, but I'm not sure what. I looked at the other answers but mine seems a little different. Here's what the relevant function looks like:
/**
* Display or retrieve the current post title with optional content.
*
* #since 0.71
*
* #param string $before Optional. Content to prepend to the title.
* #param string $after Optional. Content to append to the title.
* #param bool $echo Optional, default to true.Whether to display or return.
* #return null|string Null on no title. String if $echo parameter is false.
*/
function the_title($before = '', $after = '', $echo = true) {
$title = get_the_title();
if ( strlen($title) == 0 )
return;
$title = $before . $title . $after;
if ( $echo )
echo $title; // <-- This is line 54 of post-template.php
else
return $title;
}

My first recommendation would be to learn how to format code for SO. And figure out how to cut down your problem to the bare minimum someone needs to solve it.
My second would be to look around the line mentioned in the error. I just did, and look what I found:
if ( $echo )
echo $title;
So now you know what's outputting stuff, what can you do about it?
Well, the other part of that statement is:
else
return $title;
Now, I'm no Wordpress expert, but I'm sure that you can work out the first thing that needs changing.

You shouldn't be editing files in wp-includes without a good reason and a good understanding of what you're doing. WordPress is extendable via themes and plugins in almost all situations - you should rarely, if ever, have to hack core code to do something.

Related

Fatal error: Call to undefined function get_header() in /home/theun3wr/public_html/author.php on line 11

Since I shifted to multi-domain hosting in December, I'm plagued with errors on my website. There have been so many issues that I've lost track of them. And every day I end up with a shocking new error that I'm clueless about.
One of the most recent ones being not able to access author profile on my website - http://www.theunbiasedblog.com/author/nikhil
Rest of the website is working alright except these things:
Author profiles (Mentioned Above) this error
Fatal error: Call to undefined function get_header() in /home/theun3wr/public_html/author.php on line 11
Tag links on the site (http://www.theunbiasedblog.com/tag/windows) give this error
Fatal error: Call to undefined function get_header() in /home/theun3wr/public_html/tag.php on line 11
Category links on the site (theunbiasedblogcom/category/tech)
Fatal error: Call to undefined function get_header() in /home/theun3wr/public_html/category.php on line 11
I'm not able to figure out what to do with get_header() that is on line 11 of all these non functional links. As a result I've 30K 404 errors from google.
#prakashrao
In wp-includes/general-template.php
I've this -
/**
* Load header template.
*
* Includes the header template for a theme or if a name is specified then a
* specialised header will be included.
*
* For the parameter, if the file is called "header-special.php" then specify
* "special".
*
* #since 1.5.0
*
* #param string $name The name of the specialised header.
*/
function get_header( $name = null ) {
/**
* Fires before the header template file is loaded.
*
* The hook allows a specific header template file to be used in place of the
* default header template file. If your file is called header-new.php,
* you would specify the filename in the hook as get_header( 'new' ).
*
* #since 2.1.0
* #since 2.8.0 $name parameter added.
*
* #param string $name Name of the specific header file to use.
*/
do_action( 'get_header', $name );
$templates = array();
$name = (string) $name;
if ( '' !== $name )
$templates[] = "header-{$name}.php";
$templates[] = 'header.php';
// Backward compat code will be removed in a future release
if ('' == locate_template($templates, true))
load_template( ABSPATH . WPINC . '/theme-compat/header.php');
}
make sure you have this function defined in your active theme
function get_header( $name = null ) {
/**
* Fires before the header template file is loaded.
*
* The hook allows a specific header template file to be used in place of the
* default header template file. If your file is called header-new.php,
* you would specify the filename in the hook as get_header( 'new' ).
*
* #since 2.1.0
* #since 2.8.0 $name parameter added.
*
* #param string $name Name of the specific header file to use.
*/
do_action( 'get_header', $name );
$templates = array();
$name = (string) $name;
if ( '' !== $name )
$templates[] = "header-{$name}.php";
$templates[] = 'header.php';
// Backward compat code will be removed in a future release
if ('' == locate_template($templates, true))
load_template( ABSPATH . WPINC . '/theme-compat/header.php');
}

Wordpress PHP warning: Creating default object from empty value in wp-admin\includes\post.php on line 627

I am using Wordpress 4.2.2, Xampp installation in windows with PHP 5.6.8
I have added the following function to my theme's functions.php to automatically set the title of the post type 'departure':
function custom_title() {
if ($_POST['post_type'] == 'departure') :
$post_title = 'Departure-'.$_POST['post_ID'];
return $post_title;
endif;
}
add_filter ( 'title_save_pre', 'custom_title' );
Title setting for the custom post type "departure" works fine. But when I try to add a new wordpress standard post (i.e. post_type = post) I get the following error:
Warning: Creating default object from empty value in wp-admin\includes\post.php on line 627
Line 627 is as follows:
/**
* Filter the default post content initially used in the "Write Post" form.
*
* #since 1.5.0
*
* #param string $post_content Default post content.
* #param WP_Post $post Post object.
*/
$post->post_content = apply_filters( 'default_content', $post_content, $post );
All the other post types (i.e. custom types and wordpress standard page) work fine. By dumping $_POST data in a logfile I have seen, that for all the working post-types an empty array (i.e. array()) is passed as $_POST data. This does not happen with the "post" post type.
If I comment the function in functions.php, an empty array is sent as $_POST data when adding a new "post" post type, and the problem disappears.
Any ideas?
Thanks
You need to return a value, even if the post type isn't departure. The function should take an argument, and return something:
function custom_title($post_title) {
if ($_POST['post_type'] == 'departure') {
$post_title = 'Departure-'.$_POST['post_ID'];
}
return $post_title;
}
add_filter ( 'title_save_pre', 'custom_title' );

strpos() on title in a metabox in wordpress fails to find '-'

I was coding a simple metabox for wordpress and have a little issue when saving data.
The meta box is in my "create article"-page has two textfields. These are saved as post-meta, when the post is saved.
While saving I check if the fields were filled - if they are empty I take the post title and extract the data I need. The idea is to take everything that is before the first "-". If there is no minus sign, the whole title should be saved in my custom field. Now, this fails to find "-" in the title (alltough there is one) and returns the whole title every time:
function get_from_title($title) {
$pos = strpos($title, '-');
if ($pos) {
return trim(substr($title, $pos));
}
else {
$pos = strpos($title, '–'); //added this since two different signs could be used
if ($pos) {
return trim(substr($title, $pos));
}
else {
return $title;
}
}
}
the function that is calling get_from_title is getting the title via get_the_title( $post_id ) and this works without problems.
Is wordpress encoding the title somehow? Why can't strpos find the minus sign? What should I look for instead?
Thanks
I replied to the thread you started on this topic in the WordPress forums. You can find your answer there.
Alternatively, here's what I said. :)
Ah yes. This is a tricky one. So, why can't strpos find a hyphen in
the title when clearly we can see one? Because there isn't one. hehe.
What WordPress is doing here is converting your hyphen ( minus sign )
into an en-dash.
This will give you diddly-squat:
$pos = strpos( $title, '-' );
You want this:
$pos = strpos( $title, '–' );
Let me know how things turned out for you. :)

Rename files during upload within Wordpress backend

is there a way to rename files during the upload progress within the Wordpress 3.0 backend? I would like to have a consistent naming of files, especially for images.
I think an 12 (+-) digit hash value of the original filename or something similar would be awesome. Any suggestions?
Regards
But it would really be easier to do that before uploading files.
Not quite sure about that - this seems fairly easy;
/**
* #link http://stackoverflow.com/a/3261107/247223
*/
function so_3261107_hash_filename( $filename ) {
$info = pathinfo( $filename );
$ext = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
$name = basename( $filename, $ext );
return md5( $name ) . $ext;
}
add_filter( 'sanitize_file_name', 'so_3261107_hash_filename', 10 );
This filter creates a 32 character hash of the original filename, preserving the file extension. You could chop it down a little using substr() if you wanted to.
This filter runs once the file has been uploaded to a temporary directory on your server, but before it is resized (if applicable) and saved to your uploads folder.
Note that there is no risk of file overwrite - in the event that a newly hashed file is the same as one that already exists, WordPress will try appending an incrementing digit to the filename until there is no longer a collision.
WordPress Plugin
<?php
/**
* Plugin Name: Hash Upload Filename
* Plugin URI: http://stackoverflow.com/questions/3259696
* Description: Rename uploaded files as the hash of their original.
* Version: 0.1
*/
/**
* Filter {#see sanitize_file_name()} and return an MD5 hash.
*
* #param string $filename
* #return string
*/
function so_3261107_hash_filename( $filename ) {
$info = pathinfo( $filename );
$ext = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
$name = basename( $filename, $ext );
return md5( $name ) . $ext;
}
add_filter( 'sanitize_file_name', 'so_3261107_hash_filename', 10 );
http://wpapi.com/change-image-name-to-wordpress-post-slug-during-upload/
BTW:
Add filter to sanitize_file_name is totally wrong, as sanitize_file_name() function is a helper function to format string, it may be used elsewhere like plugins or themes.
function wp_modify_uploaded_file_names($file) {
$info = pathinfo($file['name']);
$ext = empty($info['extension']) ? '' : '.' . $info['extension'];
$name = basename($file['name'], $ext);
$file['name'] = uniqid() . $ext; // uniqid method
// $file['name'] = md5($name) . $ext; // md5 method
// $file['name'] = base64_encode($name) . $ext; // base64 method
return $file;
}
add_filter('wp_handle_upload_prefilter', 'wp_modify_uploaded_file_names', 1, 1);
I've made a plugin for it. I did it because i was having too much trouble with my clients trying to upload images with special characters
http://wordpress.org/plugins/file-renaming-on-upload
I implemented the same thing, I wanted a more random filename, than the original, as the site I am using this for is for pics only and all files are in one directory.
i did the following
return md5($ip . uniqid(mt_rand(), true)) . $ext;
I was really looking for a plugin that could do it properly, and finally I ended up making this one myself. It's available on my blog: http://www.meow.fr/media-file-renamer ! If you use it, please give me a feedback :) I sincerely hope it helps!
You can't autorename file with the media library function. I would recommend to rename files before you upload them. Even after uploading a file you can't rename it throug WordPress but only through FTP.
The only way to do that would be a plugin that hooks itself into the media library upload process. But it would really be easier to do that before uploading files.

setting variable in header.php but not seen in footer.php

in wordpress ,
i set a variable in header.php
<?php
$var= 'anything'
?>
but in footer.php when I echo it
<?php
echo $var;
?>
I got no thing printed ... why !>
You're not in the same scope, as the header and footer files are included in a function's body. So you are declaring a local variable, and referring to another local variable (from another function).
So just declare your variable as global:
$GLOBALS[ 'var' ] = '...';
Then:
echo $GLOBALS[ 'var' ];
I know you've already accepted the answer to this question; however, I think there's a much better approach to the variable scope problem than passing vars into the $GLOBALS array.
Take the functions.php file in your theme for example. This file is included outside the scope of the get_header() and get_footer() functions. In fact it supersedes anything else you might be doing in your theme (and I believe in the plugin scope as well--though I'd have to check that.)
If you want to set a variable that you'd like to use in your header/footer files, you should do it in your functions.php file rather than polluting $GLOBALS array. If you have more variables that you want to sure, consider using a basic Registry object with getters/setters. This way your variables will be better encapsulated in a scope you can control.
Registry
Here's a sample Registry class to get you started if:
<?php
/**
* Registry
*
* #author Made By Me
* #version v0.0.1
*/
class Registry
{
# +------------------------------------------------------------------------+
# MEMBERS
# +------------------------------------------------------------------------+
private $properties = array();
# +------------------------------------------------------------------------+
# ACCESSORS
# +------------------------------------------------------------------------+
/**
* #set mixed Objects
* #param string $index A unique index
* #param mixed $value Objects to be stored in the registry
* #return void
*/
public function __set($index, $value)
{
$this->properties[ $index ] = $value;
}
/**
* #get mixed Objects stored in the registry
* #param string $index A unique ID for the object
* #return object Returns a object used by the core application.
*/
public function __get($index)
{
return $this->properties[ $index ];
}
# +------------------------------------------------------------------------+
# CONSTRUCTOR
# +------------------------------------------------------------------------+
public function __construct()
{
}
}
Save this class in your theme somewhere, e.g. /classes/registry.class.php Include the file at the top of your functions.php file: include( get_template_directory() . '/classes/registry.class.php');
Example Usage
Storing variables:
$registry = new Registry();
$registry->my_variable_name = "hello world";
Retrieving variables:
echo '<h1>' . $registry->my_variable_name . '</h1>'
The registry will accept any variable type.
Note: I normally use SplObjectStorage as the internal datastore, but I've swapped it out for a regular ole array for this case.
I know this is a bit old question and with a solution voted but I though I should share another option and just found a better solution (that works) without using Globals
function fn_your_var_storage( $var = NULL )
{
static $internal;
if ( NULL !== $var )
{
$internal = $var;
}
return $internal;
}
// store the value
fn_your_var_storage( 'my_value' );
// retrieve value
echo fn_your_var_storage(); // print my_value
try this code
first define your initial variable
$var="something";
then use the $_GLOBALS
$_GLOBALS['myvar']=$var;
and finally use the global variable in anywhere you want
global $myvar;
define string inside the $_GLOBALS as taken as global variable name or use the $_GLOBALS['myvar'] direct into the code without using the global
In wordpress Header, any template, Footer is different functions so you have to declare any varible as a global variable then you can access it .
/** header.php **/
<?php
global $xyz;
$xyz="123456"; ?>
/** Template.php or Footer.php **/
<?php
echo $xyz; ///return 123456
?>

Resources