I am developing on a Wordpress website with my own php coding. So far I am using the Snippets plugin which I like most for adding PHP code to existing wordpress sites.
The only thing I would like to know is how i can use something like global constants to avoid hard-coded values. Because I am using the same values again and again. What is the best way?
Thank you for any help.
best,
It is not recommended to create your own global variables on Wordpress (read this). But you you can achieve this by defining global variables.
function my_globals() {
global $myglobals;
// We define it as an array so you can store multiple values. If only needed one value you can directly set it
$myglobals = array();
$myglobals['first'] = 'This is first content';
$myglobals['second'] = 'This is second content';
}
add_action( 'after_setup_theme', 'my_globals' );
Now you can call your global using:
global $myglobals;
echo $myglobal['first'];
echo $myglobal['last'];
Related
I have a custom page in my theme where I am creating a post from a different DB table and I want to then make this display inside wordpress as if it were a normal post. It seems to work most of the time, but randomly it seems that the post is modified after i call get_header() and i cant see why.
I have setup a mod rewrite rule in apache to hit a php file and in here i get the data I need and build the php object. I populate the wordpress post object using this function.
function BuildSimplePost($title, $body)
{
require_once( SITEROOT . '/wp-load.php' );
#$post = new WP_Post();
#$post->ID = -1;
$post->post_parent = 0;
$post->post_title = $title;
$post->post_name = "simple-response";
$post->post_excerpt = substr($body, 0, 100);
$post->comment_status = "closed";
$post->ping_status = "closed";
$post->post_type = "page";
$post->post_content = $body;
return $post;
}
Once the post is sorted I then push this post into the wordpress query objects and let the theme do the rest of the work.
$posts = array($post);
$wp_query->posts = $posts;
$wp_query->post_count = count($posts);
$wp_query->found_posts = count($posts);
$wp_query->max_num_pages = 1;
$wp_query->post = $post;
$wp_query->is_404 = 0;
$wp_query->is_singular = 1;
$wp_query->is_single = 1;
$wp_query->page_id = $post->ID;
include get_template_directory()."/external-page.php";
Inside external-page.php there is nothing weird. It is literally a copy of single.php with some html removed. The issue im having is that when get_header(); is called in this file the post is changed to something else. This then triggers a redirect which brings the user to a different page on the site that is part of wordpress.
Anyone know why the get_header method would be causing a redirect like this?
It's hard to tell exactly what's changing up your post. The way I see it, you've got two options:
Keep putting your content back into the post. This is not the WordPress way of solving this problem but it's probably the easier approach. #ArtisticPhoenix's answer above is the right concept but the wrong specific code.
Create a custom post type, which is the WordPress way. #CBroe offered this and the response to your answer to his question should (IMHO) be: "it doesn't matter how complicated your post type is, you should encapsulate it in a CPT if you're using WordPress."
Put your content back
Keep a copy of your post object somewhere and put it back right after anything that changes it. You can create a new global:
global $mypost;
$mypost = BuildSimplePost(...);
If you know the $title and $body parameters, you can just call your function again every time you need it. You could also use a transient, a update_option/get_option call, or several other things.
There are several filters that could be the cuplrit:
The the_post action should change $post. That's the one thing it really does. You may want to sit on top of the hook stack like this: add_filter('the_post', function() { global $post, $mypost; $post = $mypost; }, 10000);
Before the wp object is set up, there are several actions and you run a high likelihood of anything hooked changing $post because it's free game before then. You may have plugins or your theme hooking on: parse_request, parse_query, pre_get_posts, or posts_selection. You may try something like add_filter('parse_request', function() { global $post; $post = $mypost; }, 10000); for each of these filters.
I've seen some plugin developers adding things that change the $post global (and other things they probably shouldn't change) during calls to the actions template_redirect, get_header, loop_start, and after query. Heck, I've done it myself a few times. You could try the same thing with these filters although I'd consider them less suspect.
If that doesn't get your content back, you could try something really dirty, like forcing it in right before the call to the_post. There's no hook for this but if you control the theme it's easy.
The WP Way
I understand that you think that your post is very complicated but I'll offer you this from several years of being neck-deep in WP code: breaking the WP post model is a recipe for heartache. If you're not using posts and you're not using the way they're intended, you probably shouldn't be using WP. There are other frameworks that offer things like user authentication, themeing, database persistence, eventing, REST, etc. without the overhead of WP.
Another way
That having been said, I've used WP in the past because it was part of a requirement that I couldn't control. When my post gets too complicated, I create a wrapper object like so:
class MyPostWrapper {
private $_Post;
private $_Meta = array();
public function __construct($post) {
$this->_Post = get_post($post);
$this->_Meta = $this->_LoadMeta();
}
private function _LoadMeta() {
// Load everything into $_Meta: other tables, options, post meta, etc.
}
public function __get($name) {
if (array_key_exists($name, $this->_Meta))
return $this->_Meta[$name];
}
// Use a similar implementation for __set and __isset.
}
However complicated your post gets, you can manage its lifecycle and all of its trail very easily with a single wrapper.
Then you don't have to worry about who's eating your post. If they are, they probably should be.
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 have read many tutorials about passing multiple variables to a post/page in wordpress, using add_rewrite_endpoint. I am currently passing 2 variables, calendar_year / calendar_month . It works good for both but separately each one. Using both like (http://myserver/mysite/availability/calendar_year/2016/calendar_month/10/) only gets the year and if I pass them on reverse, only gets the month. Availability page shows correct (No 404 error), but it fails taking the last parameter when there are the two of them. Here is my code for add_rewrite_endpoint
function booking_calendar_add_endpoints() {
add_rewrite_endpoint('calendar_year', EP_ALL);
add_rewrite_endpoint('calendar_month', EP_ALL);
}
add_action('init','booking_calendar_add_endpoints');
And this is how I get it and use it:
global $wp_query;
$year_var = $wp_query->get( 'calendar_year' );
$month_var = $wp_query->get( 'calendar_month' );
I am sure I am missing something. Does anyone have a solution for it? Thanks.
I stored several values in new fields of my plugin in the settings.php
However now I'm trying to put those values that I made onscreen, and I couldn't find a way to do that in moodle. Is there a way to do this in Moodle?
Any help is greatly appreciated. Thanks!
If you named the setting in settings.php something like 'PLUGINNAME/SETTINGNAME' (e.g. in the enrol_manual core plugin has a setting called 'enrol_manual/expiredaction'), then you can retrieve a single setting via:
$value = get_config('PLUGINNAME', 'SETTINGNAME');
$value = get_config('enrol_maual', 'expiredaction'); // For example.
If you want all the settings for a given plugin, then you can call:
$values = get_config('PLUGINNAME');
$values = get_config('enrol_manual'); // For example.
If, however, you've followed the bad practice of several of the settings for older core plugins, and the setting is called something like 'MYPLUGIN_SETTINGNAME', then you can retrieve the setting by calling:
$value = get_config('core', 'PLUGINNAME_SETTINGNAME');
$value = get_config('core', 'forum_displaymode'); // For example.
OR
global $CFG;
$value = $CFG->PLUGINNAME_SETTINGNAME;
$value = $CFG->forum_displaymode; // For example.
Naming settings without the '/' is bad, as it means the settings are loaded into the main $CFG global, which is already pretty bloated. Organising them into plugins also means all the plugin settings can be loaded as a simple object.
I think I'm getting really confused with return'ing and echo'ing variables.
I've got this gravity forms hook from their support...
add_filter('gform_field_value_facebook_name', 'my_custom_population_function');
function my_custom_population_function($value){
return 'boom!';
}
This works and returns 'boom!' as my form field default variable.
This is pretty straight forward for a general text string, but I am trying to return a PHP variable instead.
I am loading the facebook PHP SDK in my functions.php at a higher scope than the gravity form hook. The facebook SDK definitely works, for example I am currently echoing this in my wordpress theme files...
echo $userData['name']
But my question is, why does it not work if I try and return the above variable inside the gravity for hook?
Please see what I have tried below, but it returns nothing...
add_filter('gform_field_value_facebook_name', 'my_custom_population_function');
function my_custom_population_function($value){
return $userData['name'];
}
I've also tried something similar in my wordpress functions.php, when trying to echo a variable in a filter...
$fb_app_id = '12345678910';
// APP ID FILTER
add_action( 'fb_app_id', 'echo_fb_app_id' );
function echo_fb_app_id() {
echo $fb_app_id;
}
But this returns nothing and the scope is the same.
Can anyone please enlighten me to why I can't pass these variables around. I think thats the technical term. Thank you very much.
This is because in PHP, functions don't read global variables without the global keyword.
$fb_app_id = '12345678910';
// APP ID FILTER
add_action( 'fb_app_id', 'echo_fb_app_id' );
function echo_fb_app_id() {
global $fb_app_id; // tells PHP to use the global variable
echo $fb_app_id;
}
Try to add global $userData; to your my_custom_population_function.