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');
Related
In wordpress how can we use functions like is_user_logged_in(), we can use this function in any page like header.php , sidebar.php
In wordpress page we are making a form which will be submitted using ajax. Suppose we are gatering form's data on a page 'submitform.php' whose url is www.mysite.com/submitform.php which is totally a custom page.
Now how will I be able to know using is_user_logged_in(); function whether user is logged in or not. because submitform.php is a simple page outside wordpress.
At the top of your custom PHP file, use the following to load WordPress core functionality. This assumes your file is in the root of the installation:
define('WP_USE_THEMES', false);
require('wp-blog-header.php');
I am developing a plugin for wordpress that loads javascript in a page.
But i want this plugin to load only on selected pages. Not all pages.
Can someone suggest how to do that?
Here is the code.
add_action('wp_enqueue_scripts', 'soundninja_enqueue');
function soundninja_enqueue($hook)
{
wp_enqueue_script('soundninja', // id
'http://soundninja.github.io/SNtest/build/Soundninja.min.js', // path
array('jquery'), // dependencies
0, // appends ?ver=$wordpress_version
true // in_footer
);
}
Another possible workaround would be to keep the plugin deactivated and activate it only for the required pages. This would prevent the additional overhead involved in loading the plugin on pages where the plugin is not required. And most importantly you do not have to tweak the code of the existing plugin.
Here is the post which can give you more idea http://shibashake.com/wordpress-theme/how-to-selectively-load-plugins-for-specific-pages
Depending on where you will call your function soundninja_enqueue($hook) you can easily add an if statement asking if the current page/post id is in an allowed list of ids.
But first you need to get the current page/post id, for this you have a couple of options, depending if are calling the function inside or outside of The loop.
see here on how to get the current page id in wordpress
<?php
$allowedIds = array(12,13,14);
if (in_array($currentID,$allowedIds)) soundninja_enqueue($hook);
Another option is to pass the the current page/post id as a parameter to the function and do the same if test inside the function, your choice.
I need to create a form that will be called many times through application .
below is the steps i did :
1- Create a form.php page and place it in my child theme folder.
C:\wamp\www\test\wp-content\themes\designfolio-child
i wrote the below code at the top of the page
<?php /* Template Name: Hercal Template */ ?>
and then i write my form code.
2-This page calls another PHP files and Jquery file , i place those file in
C:\wamp\www\test
3- I create a wordpress page and select a template option 'Hercal Template' ,
The page run now ,form loaded and working fine but i have some issue that i need to understand.
1-What about security , is it secure to place files and database connection to test folder directly ?
2-How can i call my form inside other pages ?
3-the above steps enables me to create a template page , so what is the difference between template page , plugin , short code that refer a function ???
I mean :
when i create a plugin , i can call it anywhere by using function name.
when i create a function,place it into wp-content and create shortcode for it , i can call it anywhere by using shortcode .
so what is the difference between them , i have conflict between (page template,plugin,shortcode).
Thanks
You should use shortcode to display form.
if you will use template than you can't put extra content/design for multiple places.
if you will use function than you have to call that in your page or you need to physically edit template file.
if you will use your form as shortcode than you can access/call it anywhere in your page/post/custom post and place it on anywhere between the div, paragraph etc.
Even you can access it on php file as well
I suggest you to use short code
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
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');
}