php/wordpress include a file - php

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.

Related

How to use a variable in an ACF template and pass it through a file?

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.

WordPress include file at header not working

I tried to include some php files in header.php. I use the script below :-
get_template_part( 'custom/tos_functions');
However get_template_part doesn't work for me, so I use include which works fine.
include (TEMPLATEPATH . "custom/tos_functions.php");
The issue is, if I put this line in header.php, I want to use some of the functions in custom template , say profile.php. In profile.php the file is as though not being called at all. I can't get the data I need in profile.php.
Then I tried to take out the include script from header, and place it in profile.php and the data I need can be called perfectly fine.
This will be an issue as there will be many custom php pages I need to create, thus every page will call the include script.
Question, why is it that the file cannot be called from header.php ? The data can only be retrieved within header.php and any custom page that calls the header, couldn't get the data from the include file.
Anyway I can fix this so that I can place the include file in the header.php?
Thank you!
Rather than including files in the theme template files (such as header.php), you should include them in the theme's functions.php file.
Including them in the template files is almost always too late in the WP run for it to be useful, and it may cause unexpected results and unexpected output in your templates.
Instead, in your functions.php file, do something like so:
require_once 'custom/tos_functions.php';
Then, your API code is available where you need it, and it is in the "right" place and consistent with the "WordPress way".

Saving the Wordpress header to a php file accessible anywhere?

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.

Using nextgen variable in header.php file

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

How block and template work in Magento

i've been working with magento for a while and there is some detail that i'm trying to understand how it works, block and templates, for example, the part i don't understand how works is that you can do this in a template( a .phtml file)
$this->getFunctionName();
this means that there is a function with that name in the block that was assigned that template.
I'm trying to write a simple example just to see how it works but i can't figure it out, untill now, i just have a headeach.
How is posible that you can use $this within a .phtml file to call the block functions?? Seems to be that the .phtml is part of the object, right?
thanks
Check out the fetchView method of Mage_Core_Block_Template, template files are included within that method and have access to the class. Output buffering is used to collect the template output, rather than displaying the template as it's included.
When a file is include/require'd in PHP, you can for most purposes suppose that the code inside of it gets inlined into the calling file. Hence, all scope (including $this gets inherited by the template file.

Categories