Error in simple function call in Wordpress page - php

I am trying to insert a secondary header beyond the main.
the main thing is header.php and call with get_header();
To try to make another header I added in functions.php:
function secondary_header() {
echo '<p>This is a test</p>';
}
add_action('get_customheader', 'secondary_header');
and in the page.php
get_customheader();
I have the error:
Fatal error: Call to undefined function get_customheader()

As the Wordpress Codex say "Custom header is an image that is chosen as the representative image in the theme top header section."
So, this function is expecting an array of default arguments to render the image, like "width", "height", "default-image" (image location/path).
This means your problem is one of the followings:
You are using an unnecessary function for what you want to accomplish.
You want to use the Custom Hearder function, but don't understand how it works.
To make it work you just need to delete this line:
add_action('get_customheader', 'secondary_header');
And then call your function on your template file like any other normal function:
<?php secondary_header(); ?>
If what you want is to insert additional code/html in multiple pages dynamically or just to keep your documents neat and clean, I'll recommend you to learn about the built-in function of Wordpress to insert template files, which is get_template_part().
For further understanding of how get_template_part() works I'll recommend you to learn about include() and require() which are native PHP functions and will work even outside of WP.

I think it would be easier for you to create a .php file with your custom header, and then just pull it in your template file where you call get_header() with something like
get_template_part('additional/custom_header');
This will search the custom_header.php file in the 'additional' folder of your theme.
Your error comes from the fact that you have added an action to a non existing function get_customheader(). What you wanted probably is do_action('get_customheader');
Read more:
do_action()
add_action()

Related

How to avoid `Call to undefined function` fatal error in a custom PHP file for WordPress

We're in WordPress. We're trying to make a customizable CSS style sheet. Using Advanced Custom Fields plugin, we've created a custom field called color. We'll use it as an option to define a color value, in HEX format, as used in CSS.
We've created a PHP file just to be used as CSS style sheet. It's loaded from our WordPress as a CSS resource. This PHP produces a CSS code where we want to use our color option as color property for some elements.
But, here is the problem, that PHP generates this error:
Fatal error: Uncaught Error: Call to undefined function the_field()...
This is the PHP code:
<?php
header('Content-type: text/css');
the_field('primary_theme_color', 'option');
$color = the_field('primary_theme_color', 'option');
?>
:root {
--primary-color:<?php echo $color; ?>;
}
* {
background-color: var(--primary-color);
}
We know that the_field() function exists inside Advanced Custom Fields plugin, so why it doesn't work?
Error info can't be clearer: you are pretending to use a function that is not defined. This function is the_field(), and yes, this is a known function from Advanced Custom Fields plugin for WordPress, but this is what it happens:
You have made a PHP script to generate a CSS style sheet. Inside this script, you need to use the_field() function. This script runs as standalone, so, when you try to use that function, it doesn't declared. You need to load WordPress before.
Just add this line before invoke the_field() function:
require_once 'pathToYourWordPressRootDir/wp-blog-header.php';
Of course, you need to replace pathToYourWordPressRootDir with correct value. For example, if your PHP file is inside wp-content/themes/yourTheme/assets/common/, then it will be:
require_once '../../../../../../wp-blog-header.php';
Or, more reliable:
require_once dirname(__FILE__) . '/../../../../../../wp-blog-header.php';
Note.- In many posts, you can find that the way to load WordPress is requiring wp-load.php instead of wp-blog-header.php. But I guess wp-load.php is heavier, so I prefer wp-blog-header.php, and I tried yet using it and it works fine!
Remember.- Advanced Custom Fields plugin must be installed and active.
Important
The function the_field() doesn't return anything, just does an echo of get_field(). So, to get the value you want, you need to use get_field() function:
$color = get_field('primary_theme_color', 'option');

Include external php inside Wordpress Header

I am trying to do something simple but it won't work.
I have a custom Wordpress header. I need to include external php classes and other stuff. The problem is that if I do that the page breaks, it doesn't work anymore.
This doesn't occur when I try to "require" external php scripts inside page templates. The problem occurs when I include it in the header.
To make it shorter, I have:
header-home.php (standard wordpress theme header file, which has to include the following file)
snippets.php (a php class)
header.home.php is located at /wp-content/themes/twentyseventeen-child/templates...
snippets.php is located at /resources/scripts/snippets.php
Perhaps the header loads something that is not compatible with custom inclusions?
I was able to include an external php file which contains pure html elements. If I try to load custom classes, the page simply breaks. The include filepath is correct, so that's not the problem.
All the files that you are going to require, you must do in the file "functions.php" that is in "/wp-content/themes/twentyseventeen-child/functions.php", then in the file "header.php" you can instantiate your objects or call functions. For example:
Functions.php
<?php
require("./YourClass.php");
function getYourClass() {
$yourClass = new YourClass();
var_dump($yourClass); //Verify if it returns data and then delete this line
return $yourClass;
}
Header.php
getYourClass();//Here it brings the object and shows the output.

Wordpress plugin that overrides function in wp-includes file

This is my first time attempting to make a Wordpress plugin. I would like to insert three lines of code into the post-template.php file under wp-includes. I am thinking I will need a way to override the function, in this case, get_the_content, so that it calls my plugin's version of the function instead of the default one.
Based on what I've searched, it is not a good idea to override the core files so I'm hoping there is a simple way to modify the function through my plugin.
What would be the best method to go about doing so?
You could overwrite it using the same function name inside your plugin, also you could create a child theme and inside that you can copy the file post-template.php so it will load the edited version instead without updating issues, or it may be possible (if its plugable) to overwrite or edit it using a hook, check out this answer: https://stackoverflow.com/a/17202451/7862006
You got that right. Never modify the core files directly for these reasons. However, luckily there are different ways to achieve what you want in WordPress. The simplest would be to add a filter in functions.php (if you are using a theme find the functions.php inside ../themes/..) like this:
add_filter('the_content', 'new_content_filter');
function new_content_filter( $content )
{
$new_content = 'hello';
return $new_content;
}
This will get the content and apply a new filter on it.

wordpress functions in function.php not being called from a template file

I have created a child theme and recently installed woocommerce.
I have added a sidebar-shop.php file to template, which works fine when I put dummy content into it.
In my functions file I have:
function lls_woo_before_widget(){
$content='<aside class="rightHandColumn">';
return $content;
}
add_action('init','lls_woo_before_widget');
and in sidebar-shop.php I have:
lls_woo_before_widget();
dynamic_sidebar('secondary-aside');
The function lls_woo_before_widget doesn't seem to work but the sidebar comes in fine.
Why can't I use the function from within sidebar-shop.php and how can I (I know I could just write the code into the sidebar-shop.php file...)
Thanks
If your goal here is to wrap the secondary-aside sidebar in an <aside> tag, you should be able to do this directly in the sidebar-shop.php file like this:
?>
<aside class="rightHandColumn">
<?php dynamic_sidebar('secondary-aside'); ?>
</aside>
<?php
As for the errors you mentioned above:
init is way too early to be calling this function, init hooks shouldn't generate any output since the headers haven't been sent yet, and init hooks don't modify the_content.
the_content is used to output posts in the loop, and won't end up in the sidebar as you expect.
dynamic_sidebar() prints the sidebar markup. If you wanted to act on the widgets in this sidebar as they are rendered, it has hooks
If you want to call a php function from one file in another php file, you need to include the file that contains the function before you attempt to call it.

Call a function on every wordpress pages only

I have written a security function.
I want to call this function in all pages which are created by wordpress back end only.
(Does not want want to called by all php files).
Is there any hook available?
you can use in a php file (like header.php or functions.php) like this
if(is_page())
{
// code to execute on all pages
}
You can tie it to the 'wp_head' hook.
http://codex.wordpress.org/Plugin_API/Action_Reference/wp_head

Categories