Cristiano Matos
Cristiano Matos

Reputation: 329

Error in simple function call in Wordpress page

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()

Upvotes: 0

Views: 59

Answers (2)

Luis Rivera
Luis Rivera

Reputation: 517

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.

Upvotes: 2

dingo_d
dingo_d

Reputation: 11670

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()

Upvotes: 1

Related Questions