In Wordpress site i got nextgen gallery plugin's variable - $gallery.
It holds everything i need. It is in wp-content/plugins/nextgen folder nggfunctions.php file i think.
I need to use this variable in my wp-content/themes/twentyten/header.php file.
How do i do that?
The ugly solution would be to use the global statement to promote it to your current scope.
For more information check: http://nl2.php.net/manual/en/language.variables.scope.php
Related
I use wordpress and ACF. I use $link in the template, for example:
$link = get_field('custom_link');
After that, I wrote a code that checks the url for identifiers and if there are matches, then replaces $link['url'], $link['title'] and $link['target'] with "options".
Next comes:
<?php echo esc_html($link_title); ?>
This all works in a build, but I'm trying to use the same code on all links in all templates, so I created a "links.php" file and pasted my link checking code in there.
In the template, I tried to call the file with get_template_part and include. Also, I've tried include(locate_template('links.php')), but that doesn't work either.
Can someone tell me how to properly use repeatable code in ACF templates?
Everything worked using:
include(locate_template('folder1/links.php'));
The template was located in the folder with the theme in "folder2", and the link file in "folder1".
Before that, I tried:
include(locate_template(' ../folder1/links.php'));
and various variations, but I did not think that the path is taken from the theme folder.
In my wordpress plugin I have a function which has a page id:
public function create_view_for_pdf() {
$page_id = $_POST['page_id'];
$this->template_shortcode('template.php');
}
here, "template_shortcode" function includes a template located in a folder in the plugin directory.
private function KendoPdf_template_shortcode($template_name) {
return include '/template/dir' . $template_name;
}
In the template file, I want to pass the page id so that I can print content there. How can I do that?
Note: Since I am just including the template file, I thought I will get the $page_id variable there normally. But it did not work. I needed the page id in template file because the content will be different than the actual page. That page also has ACF fields. I am basically creating a new template for pdf export. That's why I cannot use all the content of that page.
Please correct me if I am wrong but why would you pass the the page_id to the template file if it is within $_POST? Just access the variable using $_POST['page_id']; within your template file.
Instead of including your template file you could also read it into a string with file_get_contents(); and do your desired replacements before you return it.
Another possibility would be a global variable which is already set: global $post;
And last but not least you could use output buffering with ob_start(); (and consecutive functions).
You see: A lot of ways to solve this.
The file is manually created by me & not a part of the theme.
I've tried by placing get_header( ) inside my file but it shows a error that get_header( ) is not defined.
Maybe you don't have default header and footer names. Then you need to set additional name.
Includes the header.php template file from your current theme's directory. If a name is specified then a specialised header header-{name}.php will be included.
If the theme contains no header.php file then the header from the
default theme wp-includes/theme-compat/header.php will be included.
Source: https://codex.wordpress.org/get_header
I highly discourage it but you need to include the wp-load.php file that resides in the root of your wordpress installation, in your lone php file. That is how you have access to WP functions and capabilities.
But again I HIGHLY DISCOURAGE IT. Find another way, create a simple plugin and activate it, in which you put that php file's code, or if part of a theme, just create a new page template with a different header, that you can call with get_header('name_of_header_without_php_extension') or a new footer called the same way but with get_footer().
There are alternatives, maybe if you share what you are trying to achieve, we can guide you to a better, safe solution.
Based on theme it's defer.
Same time define as get_header(),get_footer()
But now latest themse assign particular theme name based header & footer function
Ex: Theme Name is "Demo"
So the header function assign as Demo_get_header() or get_header_Demo()
Dashboard ->Appearance ->Editor -> in your Template file or in page.php write
<?php get_header()?>,<?php get_footer()?>
if you want to use different headers on different pages
1. create new file , name it header-example.php
2. you can call this header in any page using code
I have been searching all day to figure this out and still cant find an answer. I want to take the header of my wordpress site and have its own file (headerstandalone.php). I want it to do all the functions and calling it needs to do without being in the same directory as the standard header.php. Is this possible?
I am basically trying to implement it into some custom pages.
Would this help you with your problem?
<?php include_once("headerstandalone.php"); ?>
I would put the headerstandalone.php in a seperate directory, be sure to include wp-load.php in your standalone file so you can use all your functions and then include that file in your main header using require_once(../another_directory/headerstandalone.php).
If you don't want to include the file in you header then you could add the standaloneheader.php code to a function and add it to either your own plugin's function file or the themes function file and call that function in the header.
This question is regarding wordpress.
I have two variables $random_n and $adcount declared in each of index.php , archieve.php, search.php and single.php files at the very first line.
The variables are declared as follows:
<?php $random_n = randomNumber();?>
<?php $adcount = 1; ?>
randomNumber() is a function inside function.php.
If I move those variables to header.php and remove them from all these files, it stops working. :/ The variables can't be found in index.php, archieve.php, search.php, single.php if they are placed in header.php
I've also tried putting this code inside a new file and including that file to index.php, archieve.php, search.php, and single.php, but it hadn't helped as well.
If the variable is somehow related to your blog configuration, you should use the wordpress option API to build this (see here , search for option).
If your variable is random (your function name hints that) a way to work around this would be using session variables to store those values.
A good tutorial on how to use session variables in wordpress can be found here:
http://www.myguysolutions.com/2010/04/14/how-to-enable-the-use-of-sessions-on-your-wordpress-blog/
Type in var_dump($random_n) into the beginning of index.php and post the output, please.
One possible reason is that your index, search and so on are included inside the function that does not share the scope with header.php, so your vars are just outside of the scope. It might help if you declare them global both in header.php and your working files. But that's not a good idea.
Better solution would be to introduce some Registry object (Registry pattern) and use it.