Call a function on every wordpress pages only - php

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

Related

Include PHP files depending on Wordpress Templates being used

I am using WordPress for my site. The site uses many different PHP page templates to display information to the user. At the moment I include all PHP files necessary for posting data to the backend of the site in a common functions.php file e.g. require_once("function1_functions.php"); require_once("functions2_functions.php"); etc
The issue I am having is all of my functions#_functions.php files are loaded on every page, I don't want this. There is an IF statement you can use in WordPress that will allow you to put call certain files on a page depending on the template (page) the user is currently on but this doesn't work for PHP files.
if (is_page_template( array('pagetemplate-about-us.php') )) {
// call JS files here that will only show on the "About us" page
require_once("functions_about_us.php"); <-- This doesn't work.
}
Any ideas?
The Conditional Tags which control what content is displayed with conditions (such as is_page_template(). Note these only work after the posts_selection action. So here the setup function is actioned at wp action, just after posts_selection.
More info here on Conditional Tags.
if (!function_exists('theme_php_include_setup')):
function theme_php_include_setup() {
if (is_page_template( array('pagetemplate-about-us.php') )) {
require_once("functions_about_us.php");
}
}
// apply conditions at appropriate WP action
add_action('wp', 'theme_php_include_setup');

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

Trying to use WP shortcode in HTML

I'm trying to make a shortcode OUTSIDE POST/PAGE in WordPress.
The problem is, when I'm placing the do_shortcode() function, HTML automatically document the PHP code (e.g <!--*php code* -->).
What can cause the problem?
You don't actually place the do_shortcode() in your pages. Somewhere in your functions.php or shortcodes.php file you set up the function and assign a shortcode to the function.
function myFunction($params = array()) {
//function code here
}
add_shortcode('shortcodename', 'myFunction');
Then once that is done you can put [shortcodename] into your pages, inside square brackets just like that. There are some plugins that will allow you to run php and enter it right on the page, but the default shortcode functionality requires you to add the shortcode first.

Wordpress function access through url

Is there any option to access a function inside function.php through a url?
Like joomla has this.
Any idea?
Use ajax functionality of wordpress:
HERE
You can use nopriv actions for that.
wp-admin/admin-ajax.php is the url for the ajax page of wordpress.
Or either you can make a template page which gives only content and create a shortcode
and add to a specific page.
functions.php is part of the core of your theme and is automatically included.
to access your function, simply use it in your theme file, as such:
<?php myfunc(); ?>

MySQL Functions in WordPress Admin Page

I'm currently making a custom WordPress admin page via functions.php. I want this page to basically retrieve information from a SQL database (unrelated to WordPress) and display it to the visitor. I can do that fine, but if I stick the code for this in my functions.php, will that SQL code run pretty much on every page request, no matter what page? Here's my functions file pretty much:
function mytheme_add_admin() {
add_menu_page('Members', 'Members', 'moderate_comments', 'members', 'mytheme_admin');
}
function mytheme_admin() {
?>
#all the code for my page here
<?php }
add_action('admin_menu', 'mytheme_add_admin');
The "#all the code for my page here" would be HTML and bits of PHP to grab SQL data. Is there a way to make this only happen when viewing that specific admin page?
This will run a query on every page load unless you add a condition either within the function itself or (preferably) around the add_action() call. There are several ways of having it run only on a single page, but the two easiest ways are probably to use wordpress' built in global variable 'pagenow' or to use one of the $_SERVER variables to do the same ($_SERVER['REQUEST_URI'] for instance)
if (in_array($GLOBALS['pagenow'], array(pages this should show up on))){
add_action('admin_menu', 'mytheme_add_admin');
}
or
if (in_array($_SERVER['REQUEST_URI'], array(pages this should show up on with path from root))){
add_action('admin_menu', 'mytheme_add_admin');
}

Categories