I'm trying to "Import" the Wordpress core into an own script to use the functionality such as wp_query etc. I've created an script in a subdirectory (own framework) and want to extend it by wordpress, but everytime the script throws an error:
Fatal error: Call to a member function add_rewrite_tag() on a non-object in .../wordpress/wp-includes/taxonomy.php on line 333
such as (when I remove the add_action( 'init', 'create_initial_taxonomies', 0 )):
Fatal error: Call to a member function add_rewrite_tag() on a non-object in .../wordpress/wp-includes/post.php on line 1006
The non-object is the $wp_rewrite-object. I've echo'ed something and figured out that first $wp_rewrite is valid and at the next call not. I've changed nothing at the WP core files.
I try to include the core by calling:
require_once(BASE_PATH . 'wp-load.php');
Has anybody some ideas for me?
thanks
Short answer, do this:
define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require(BASE_PATH . 'wp-load.php');
Long answer, it's a subtle gotcha around importing scripts with PHP.
If you define a local variable, outside of all functions, then it can be retrieved inside a function using 'global'. If you have a local variable inside a function, it cannot be retrieved later using global, unless it is defined as being global there and then.
The script 'wp-settings.php' is where the issue lies. It is included via your call to include 'wp-load.php'.
The variables defined there are not stated as being global; instead this is presumed because the script is always run outside of any functions, and so are automatically global. i.e.
$wordpress = 'foo';
function wordpressFunction() {
global $wordpress;
}
Because you are importing the script within a function, they now become local variables. You are essentially doing:
function myFramework() {
$wordpress = 'foo';
function wordpressFunction() {
global $wordpress;
}
}
So the fix is to define them as global yourself before importing the script. Now $wp_query, and the others defined as global, are correctly found.
The easiest way to access everything wordpress has programmed in is to use the following:
require_once('/../../../wp-blog-header.php'); // Use actual root path to wp-blog-header.php
header("HTTP/1.0 200 OK");
Using the above code you'll get all functions you would normally get using a template with in WordPress. I've tried all the other methods listed above and this one is by far the best.
I had the same error. I wanted to get some articles along with permalinks. This helped:
global $wpdb, $wp_rewrite;
require '/(...)/wp-config.php';
$result = $wpdb->get_results( $wpdb->prepare( ... ) );
foreach( $result as &$item )
$item->link = get_permalink( $item->ID );
I also found this useful in another case:
http://www.stormyfrog.com/using-wpdb-outside-wordpress/
Related
I followed the syntax the best I could to create a shortcode on execution, I got the wsod. Once removed, all was well. But I don't know what is wrong with my code. This code sits inside 'My Custom Functions', a plugin for wp.
In researching how to write a custom shortcode, I discovered instructions here: https://torquemag.io/2017/06/custom-shortcode/ My expertise is in mysql and use mostly plugins in our wordpress website. I am very limited with coding.
function last_updated_shortcode {
$last_updated = $wpdb->get_results( "SELECT MAX(process_time) FROM
qgotv.last_updated");
return $last_updated;
}
add_shortcode( 'last_updated', 'last_updated_shortcode' );
This shortcode should retrieve a max(datetime value) from a db table so it can be displayed on a page. The query works. The qgotv db is separate from the wordpress db but can be accessed through wp.
Two issues I can see, one is that you have a syntax error in your function. When defining a function in PHP, you need to include the arguments parenthesis: function my_function(){ /* Do Stuff */ }. Also, you probably need to reference the $wpdb with the global keyword.
You can read up a bit on the $wpdb class as well as creating your own functions.
This should get you sorted out:
add_shortcode( 'last_updated', 'last_updated_shortcode' );
function last_updated_shortcode(){
global $wpdb;
$last_updated = $wpdb->get_results( "SELECT MAX(process_time) FROM qgotv.last_updated");
return $last_updated;
}
I was creating a wordpress plugin where the user enters in some information, and is able to generate shortcodes. I was not quite sure where the shortcodes are supposed to go - my current setup is class-based, and I want to be able to create a shortcode when an AJAX request is being made, and is successful. The following two methods are in the same file in the class.
This method gets called via the admin-ajax.php file:
public static function processAjax()
{
global $wpdb;
$event_data = $_POST['obj'];
$event_title = $event_data[0];
$event_subdomain = $event_data[1];
$result_events = $wpdb->get_results("SELECT * FROM wp_shortcode_plugin WHERE subdomain = '{$event_subdomain}'", OBJECT);
if (sizeof($result_events)>0) {
echo "duplicate";
} else {
add_shortcode($event_subdomain, 'getEmbed');
$results = $wpdb->insert('wp_shortcode_plugin', array("event_name"=>$event_title, "subdomain"=>$event_subdomain));
echo json_encode($_POST['obj']);
}
die();
}
And here is my getEmbed() method that I would like to call.
public static function getEmbed()
{
return 'test';
}
It seems that the shortcodes are not being created, however. Am I missing something here? Also, is it possible to pass a value to the getEmbed function from the add_shortcode() method?
Instead of adding shortcode directly from AJAX, you should use update_option to store in the information for the shortcode to be loaded. If option doesn't exist, it will be created.
Than you will simple use wp_init hook to load up all of the shortcodes you need to load in the function.php file for the theme or plugin php file.
You should use get_option within the wp_init hook and check the values in there. You will need to have function(s) associated with the shortcodes, which can be autogenerated in php using create_function or you can route them through 1 function (defined in your php file) that will have the $atts and $content parameters defined and do whatever depending on the value of your get_option that you send to that function.
add_shortcode function should be defined within the wp_init hook, after checking the value of the get_option function. You will need to give your option a name and add to it via the ajax function. the option will most likely want to be an array, that wordpress will automatically serialize. Than you use that array returned from get_option to loop through the array of shortcodes, and call add_shortcode as many times as you need there. This requires setting up your option array so that it has a shortcode tag defined in each index of the array. I would, personally, make the shortcode tag the key of the array and all attributes of the shortcode, imo, would than be an array of that array.
Hope this helps you to get started on this.
Would appreciate it, if anyone can let me know how we can set the global $user variable, so that we don't have to keep re-declaring it in each function, to access its contents. How can we declare it so that all the functions in a module can use it?
The type of global you're looking for (available always, in every scope) is called a superglobal in PHP. You cannot declare new superglobals, but you can access all globals directly because they are part of the $GLOBAL superglobal. In other words, you can use $GLOBALS['user'] to access the $user global directly.
See also create superglobal variables in php? for more info and alternative methods.
You can't...that's how globals work in PHP. If you want to import the global variable into your local function then you have to use the global keyword, there's no way round it. This is a 'feature' of the PHP language, it has nothing to do with Drupal.
An alternative method might be to implement a helper function:
function get_current_user() {
global $user;
return $user;
}
And call it like this:
$user = &get_current_user();
In your function if you want to use the $user variable, you just required to use/instantiate 'global' keyword before $user it. So that you can access all the data for that current user of the website.
For example
function myGenericFunc(){
global $user;
$user_id = $user->uid;
}
Note that you cannot redeclare it.
I hope it helps.
I'm a PHP noob and have a question which seems to be simply - not as said, I'm a noob and can't solve it myself.
I have a wordpress blog running a template, and when searching without any searchresults, a error shows in the top of the page:
Warning: Missing argument 1 for get_page_id(), called in /var/www/titanen.dk/public_html/spillersmart/wp-content/themes/WPTube4/functions.php on line 262 and defined in /var/www/titanen.dk/public_html/spillersmart/wp-content/themes/WPTube4/functions.php on line 237
The functions.php can be seen here http://spillersmart.dk/functions.txt and a example of the page can be seen here http://spillersmart.dk/?s=xxx
Thanks in advance, guys! :-))
This problem sounds like it is somewhere within the theme, what the error means is: At line 262 inside functions.php this is happening.
function tube_getcustomfield($filedname, $page_current_id = NULL)
{
if($page_current_id==NULL)
$page_current_id = get_page_id(); //!HERE IS THE PROBLEM!
$value = get_post_meta($page_current_id, $filedname, true);
return $value;
}
The function get_page_id(); is being called without supplying an argument, and if you look at the definition for the function:
function get_page_id($page_name){
global $wpdb;
$page_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."' AND post_status = 'publish' AND post_type = 'page'");
return $page_name;
}
This function requires one argument. Without getting a better look at how the theme has been built I don't know exactly how the get_page_id() function is being used. I wonder whether that line of code should be changed to
$page_current_id = get_page_id($filedname);
If this doesn't work then see if you can get a more up to date version of the theme you are using.
I am trying to set up a Drupal 6 node to load blocks dynamically depending on the selected theme. I figured that I could use $theme_key to determine the name of the theme, and work from there. The weird thing is that if I have several checks on a page, the first one will return an empty value for $theme_key, but subsequent checks will work as expected.
For instance:
<?php
print "Theme: ". $theme_key;
if($theme_key =="foo"){
$viewName = 'theView';
$display_id = 'block_1';
print views_embed_view($viewName, $display_id);
}
else {
$viewName = 'theOtherView';
$display_id = 'block_1';
print views_embed_view($viewName, $display_id);
}
?>
If I have the above in the node multiple times with theme "foo" active (for testing purposes) - the first time will return a blank value for $theme_key, and display theOtherView, but the second time it will show Theme: foo and will display theView. All subsequent calls to $theme_key will be correct as well.
Any calls to $theme_key prior to the first block will return blank values.
I am declaring
<?php global $theme_key; ?>
at the beginning of the node content. (Before all of the conditional blocks...)
What am I doing wrong? Is there a better way to check the current theme?
Just looking at the init_theme() function the globals $theme and $theme_key contain exactly the same values so you could try using $theme (as yvan suggested), but as they contain the same data and are both globals set in the same function I'm not sure it would make any difference.
Is this code in a template file? If so you could be suffering from the age old Drupal theming problem whereby some variables aren't available when the template is built. You could try adding a hook_preprocess_node() function in a module/theme to set up the variable and pass it to your template file. Something like this:
function MYMODULE_preprocess_node(&$vars) {
global $theme_key;
$vars['current_theme_key'] = $theme_key;
}
And then in your template file you'll have access to the variable $current_theme_key, which should then have the right variable in it.
Hope that helps, I've come across these sort of problems with Drupal before and they're a nightmare to debug.
Edit to add more helpful function:
function MYMODULE_preprocess_node(&$vars) {
$node = $vars['node'];
if ($node->type == 'my_type') {
global $theme_key;
$view_name = $theme_key == 'foo' ? 'theView' : 'theOtherView';
$display_id = 'block_1';
$vars['my_custom_view'] = views_embed_view($view_name, $display_id);
}
}
Then in your node.tpl.php or node-type.tpl.php file you can use code like this:
if (isset($my_custom_view)):
echo $my_custom_view;
endif;
By constructing the variables in a preprocess function you shouldn't have any problems with the global $theme_key not being available any more.
Bear in mind you could also implement this in your theme (in the template.php file) by changing the function name to MYTHEME_preprocess_node.
Make sure you clear your Drupal cache once you've implemented the function (wherever you decide to put it) so the system will pick up the changes.
Hope that helps!