I have a custom php page outside of wordpress, this page is connecting also to a external database, and get resultat form a sql query on a variable
i use require('../wp-blog-header.php') and require('../wp-load.php') to integrate with wordpress.
now i need to use this variable on the functions.php of my wordpress theme.
i do on my external page :
<head>
<connect to my database>
<my query>
<get resulte on $variable>
<require('../wp-blog-header.php')>
<require('../wp-load.php')>
but when i use $variable on my functions.php it do not work ?
You need to include wp-load.php alone. It will load wp-include (and wpdb), plugins and your theme (functions.php). To use the database directly you would do this:
global $wpdb;
$wpdb->get_results('select ...');
If you need to access variable declared in functions.php, it's straight forward:
say.php
<?php
include 'var.php';
print_r( $moo );
?>
var.php
<?php
$moo= 'cow';
?>
output of say.php
$ php say.php
cow
Loading (include or require) functions.php after wp-load.php will result in redeclaration of the functions defined within functions.php (it will be loaded twice), causing PHP to crash.
Unless you are using wordpress features in your external query there is no need to include wordpress files.
Your external.php could look something like
function handy_prefix_my_processing_function()
{
//connect to database...
//Do something with data...
$result = $results
//return the result
return $result;
}
then in functions.php
function handy_prefix_get_my_variable()
{
//Load the external file and make its functions available
include_once( 'external.php' );
//Get my result
$result = handy_prefix_my_processing_function();
//Do something with the result....
}
Related
OK, so here is what I have and found (which works - however it produces a log Notice) "Only variables should be passed by reference in eval()" which is driving me absolutely crazy!
Initially like any theme region I added the regions I wanted for views use to the theme.info file.
; View Regions
regions[view_header] = View Header
regions[view_content] = View Content
regions[view_footer] = View Footer
In the views-view--page.tpl.php I placed these (where I wanted the region to render)
<?php print render(block_get_blocks_by_region('view_header')); ?>
<?php print render(block_get_blocks_by_region('view_content')); ?>
<?php print render(block_get_blocks_by_region('view_footer')); ?>
This produces a notice for each entry in the tpl yet it works as expected 100%
I then tried adding a hook in the themes template.php (in an attempt to make variables out of them) so I can just use <?php print $view_header; ?> like so:
function theme_preprocess_views_view(&$variables){
$variables['view_header'] = render(block_get_blocks_by_region('view_header'));
$variables['view_content'] = render(block_get_blocks_by_region('view_content'));
$variables['view_footer'] = render(block_get_blocks_by_region('view_footer'));
Again this works - and again this produces log notices.. this time 3 for each region on all page loads, so 9 instead of 3 (not just when the views-view--page.tpl.php loads) which traced back to theme template.php (same notice as listed above this time but no notice from the $variables used in the views template when that page loads).
Obviously I'm missing something here! Maybe this is entirely the wrong way to do this...
How do I make the theme regions usable variables in tpl's?
I'm under the impression everything I've found is years old (3 to 10) and likely worked fine for php 5.x - this site is currently using php 7.2.x (should that make a difference in how this needs to be done)
Any help here would be greatly appreciated, thank you!
This is because the function render() expects a reference, and only variables should be passed by reference :
function render(&$element) {
# code need to be able to modify $element
}
render('value'); # throws notice
render(block_get_blocks_by_region('view_header')); # throws notice
$value = 'value';
$build = block_get_blocks_by_region('view_header');
render($value); # Ok
render($build); # Ok
Also, I think the way to go should be to assign the renderable arrays to $variables in the preprocess hook :
function theme_preprocess_views_view(&$variables){
$variables['view_header'] = block_get_blocks_by_region('view_header');
$variables['view_content'] = block_get_blocks_by_region('view_content');
$variables['view_footer'] = block_get_blocks_by_region('view_footer');
# ...
}
... and let the template call render() :
<?php print render($view_header); ?>
<?php print render($view_content); ?>
<?php print render($view_footer); ?>
I have tried to implement the code which can get the user region when the user open the page but it is not working i have used this code inside the functions.php
function user_region() {
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
return '<div class="user-region">'.$details->region.'</div>';
}
add_shortcode("get-user-region", "user_region");
this is my shortcode inside the functions.php and i have used the shortcode inside the wordpress page
<?php echo get-user-region_callback(); ?>
<?php echo do_shortcode('[get-user-region]') ?>
I have tried both ways but both are not working no results are showing from this shortcode.
First of all, your URL to ipinfo is incorrect. If are treating the response as JSON, but the output of https://ipinfo.io/ip is HTML. You need to specify/json` at the end:
https://ipinfo.io/{$ip}/json
Other than that, you need to check that file_get_content is enabled on your platform and that it allows remote url loading (setting allow_url_fopen).
I too searched in all forums and even I post my question on the whmcs forum but no response. What i need is that i create an addon in modules and i want to change template (from six to five) in hooks of this addon. The aim is to change template for specific clients.
I already test to change the GET var but not working :
$_GET['systpl'] = 'five';
I also tested this but the css files don't load. It redirect me to home :
global $smarty;
$template = $smarty->getTemplateVars('template');
$template = 'six';
$smarty->assign('template', $template);
$template = $smarty->getTemplateVars('template');
Any suggestion please?
I have done this in one of my products - to get this to work, you have to pull the global $systpl variable:
global $systpl;
$systpl = $tpl;
$GLOBALS['_SESSION']['Template'] = $tpl;
$GLOBALS['CONFIG']['Template'] = $tpl;
Where $tpl is the template name you are looking to set, in your case 'five'. You have to also set the GLOBALS variables there so that the user session is maintained with that template and so that the system knows to use that template name when pulling from the config.
Hope that is of help.
In WHMCS to load another template folder for specified page, I did:
<?php
use WHMCS\Database\Capsule;
use WHMCS\View\Menu\Item as MenuItem;
define("CLIENTAREA", true);
// Set the template you want to use for the custom page BEFORE init.php is called
$GLOBALS['_REQUEST']['systpl'] = 'five';
require("init.php");
// WHATEVER YOU ARE DOING IN HERE
// Set the session back to the default template:
$GLOBALS['_SESSION']['Template'] = 'six';
$ca->output();
I want to create some variables / options to customize my wordpress theme.
At the moment I already have 5 variables which I set inside the functions.php file:
$isBoxedLayout = true;
$postCounter = 0;
$postDate = false;
$socialButtons;
$showCatLink = false;
I access them throughout the other files like header.php, content.php etc.:
global $isBoxedLayout;
global $postCounter;
At the top of the files. I think it would be better to have like only one array where I can store it all!? But how do I do it right?
Also, does somebody know how I can access a variable, set in the functions.php, inside the template-tags.php file?
I already tried this:
include '../functions.php'
But I get the following error:
include(../functions.php): failed to open stream: No such file or directory
When I want to access the variable in a file like content.php it works well. Where is the difference?
Maybe not exactly the answer you are looking for, but I would take a look at the Options API. It saves your options into the database and is very easy to use.
// Functions.php
add_option( 'isBoxedLayout', true, '', 'yes' );
// template-tags.php
$isBoxedLayout = get_option( 'isBoxedLayout' );
I have a problem with my custom theme for wordpress. I made a custom theme locally on my PC where I installed Wordpress. Now inside my theme is another folder let's say php, and inside this folder is a php file that will fetch data from the database which I want to run later on. The code in this php file is as follows:
<?php
define('WP_USE_THEMES', false);
require('/wp-blog-header.php');
header('HTTP/1.1 200 OK');
global $wpdb;
$rs = $wpdb->get_results("select * from wp_users");
$user_db=$rs[0]->user_nicename;
echo $user_db;
?>
The codes above works on my local machine, where it prints out the user_nicename field from my local database (phpMyAdmin). Now when I upload and install the custom theme I made and access this php file the browser displays nothing. Is something missing in define or require so I can successfully connect to the Wordpress database? Any help help would be much appreciated.
Please note that theme is located at C:\wamp\www\wordpress\wp-content\themes\custom_theme and the php file to connect to database is here: C:\wamp\www\wordpress\wp-content\themes\custom_theme\php\connect.php
Instead of using the above code, why not just the function get_userdata on your template where you want to display the user nicename.
<?php
$user = get_userdata( $userid );
echo $user->user_nicename;
?>
See http://codex.wordpress.org/Function_Reference/get_userdata for details and look at the "Related" section at the bottom for other related uses.