Use of undefined constant ABSPATH - php

I am using WP for the first time. I'm just trying to create a very basic script to echo the user's id and am having all sorts of issues.
The code is this and is currently located in wp-content/plugins (i'm not really sure where these things should be):
<?php
require_once ABSPATH . '/wp-includes/pluggable.php';
$user = wp_get_current_user();
echo $user->get_site_id();
I'd had it without the require initially but I was getting a function not defined error for wp_get_current_user. Now I'm getting Warning: Use of undefined constant ABSPATH - assumed 'ABSPATH'...
Is there some sort of predefined set of files that I need to include or some specific directory I need to be putting my scripts so that these variables and functions are in scope? My understanding was that these things are supposed to be global.

Did you try code like that:
add_action('init', 'some_function_name');
function some_function_name() {
$user = wp_get_current_user();
echo $user->get_site_id();
}
The WordPress comes with hooks (actions and filters) to let other developers modify either core parts of the WordPress or code from other plugins / themes.
The code I describe in my answer, it is running your code when the whole WordPress , all the Plugins and the theme are loaded, thus you should have by default the wp_get_current_user() function and you should not need to include manually the pluggable.php.
This seems like is going to solve your problem.
Read more on hooks here: https://developer.wordpress.org/plugins/hooks/.
Side note. Keep in mind that in order to run your custom code you should register a proper WordPress plugin and activate it. If you have made a php file in the plugins folder, and you loaded using PHP functions like require/include the plugin probably will not operate as you expect although the source code it could be perfect. To get more details on how to write your own plugin, you could read here: https://developer.wordpress.org/plugins/

Related

Error: Call to undefined function get_meta() in functions.php

I have inherited a custom wordpress theme and am doing a few changes to it (all cosmetics of changing city names, etc) , but at the bottom of a post I get the error : "Fatal error: Call to undefined function get_meta()"
In my functions.php I have this:
function bardetails(){
$web = get_meta('web');
$email = get_meta('email');
//...
}
I have a Java background but am new to php and wordpress. I dont see anywhere else this function is called, but must be tied to a post.
Have looked and can find a lot of problems about "get_post_meta()" and "meta()" but nothing about this. It also seems to be working on the other site the theme is on. I used FTP to pull over everything and all the other parts of the site seem to work fine. Looked at the database for something that could help with no luck, and also have tried to find if this is some kind of library, as the intention of the function is clear and thats to grab the website/email of the post.
So is this just a straight custom function? And if so were should it be defined in a php/wordpress setup?
It was due to a plugin that was not installed. "More Fields" By Henrik Melin, Kal Ström is a seemingly defunct plugin that was needed that allows you to use "get_meta()". For now, I just copied over the plugin but seems like I need to find an alternative going forward.

Including plugin files into main plugin file properly

I've been having this issue for a while but keep just working around it an thought I'd finally get it solved.
I'm trying to include files into my main plugin document (the one that has the plugin title and version in it) like this:
define('SBT_PLUGIN_URL', plugin_dir_url(__FILE__));
include(SBT_PLUGIN_URL . 'competition_table.php');
inside the competition_table.php is an add_shortcode(); function that needs to run, in order for the shortcode to be registered with wordpress:
function add_table() {
//Run code here
}
add_shortcode('competition_table', 'add_table');
When I run the code on the site the link resolves properly, including the correct file, however I get this Fatal Error:
Call to undefined function add_shortcode()
However if I add exactly the same code that is in the competition_table.php into my main plugin document then the code runs perfectly.
So basically, my question is, why is Wordpress not recognizing it's own function and how can I include the file to make the code run properly?
Thanks in advance
You have to develop with WP_DEBUG enabled. It dumps an error: wrapper is disabled in the server configuration. That lead me to this: "Trust me, you do not want to include from URLs.".
Then I realized you're defining that constant with plugin_dir_url(), when what you need is a path. The following magic constant does the job:
include_once __DIR__ . '/competition_table.php';
Thanks to the feedback from #b__ I have managed to solve this issue.
For some reason, Magic Constants don't always work with wordpress, however, you can use it's equivalent to get the same effect:
include_once dirname(__FILE__) . '/competition_table.php';
When including files for use in a wordpress plugin you should always include via a PATH, not by a URL.

include typoscript with php

Is it possible to include a typoscript file via php?
Normally I would include typoscript with this:
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/setup/1.ts">
But I want to do this just with php and not typoscript. Is that possible?
My Purpose: I want to dynamically load typoscript in my page
This can be achieved by invoking accordant functions at an early stage, e.g. in calling or delegating it in ext_localconf.php. For example, the bookstrap package is loading TypoScript in PHP like this:
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPageTSConfig(
'<INCLUDE_TYPOSCRIPT: source="FILE:EXT:' . $_EXTKEY
. '/Configuration/PageTS/Mod/Wizards/newContentElement.txt">'
);
Please consider, that TypoScript is cached before the actual front-end rendering starts. This means, that you should not modify TypoScript if you're plugin class or controller logic has been called already.
May be you need to return a value from the php function and use typoscript conditions for choosing the typoscript file.
You might try the following (if I get you right):
$typoscriptFile .= file_get_contents($someFile);
$parser = t3lib_div::makeInstance('t3lib_TSparser');
$parser->parse($typoscriptFile);
$tsArray = $parser->setup;
I really don't know how well that will play with anything related to global typoscript though.
If you wanted a complete correct parse, you might be able to pull something like this off if you populated a fresh t3lib_TStemplate instance from $GLOBALS['TSFE']->tmpl and than ran the code above. Might work, never tried.

Where to put my custom functions in Wordpress?

I am pulling my hair out. I have created some simple functions that generate random numbers, pulled from a database that I want to use on wordpress pages. (And then call them from the theme files, such as header.php or page.php.)
I have tried putting the functions inside functions.php that is in the theme (as per the documentation I have read), but I keep getting the "call to undefined function" errors! What in the world am I doing wrong?
Example, here is a function inside the functions.php
function randomPollNumber() {
///this gets a random active poll number
$sql12 = "SELECT id FROM poll WHERE removed = 0 AND active = 1 ORDER BY RAND() LIMIT 1";
$result12 = mysql_query($sql12);
$myrow12 = mysql_fetch_row($result12);
$pollquestionid = $myrow12[0];
return $pollquestionid;
}
And I am calling it, from the header.php file with this
<?php echo randomPollNumber(); ?>
And yes, I DID try using the if_function_exists, but of course it cannot FIND the function, so of course it does not exist. Please help?
Very strange - some debugging tips:
Put die('functions.php file loaded') statement at the beginning of functions.php (not inside a function). If it doesn't die, you know the file isn't being loaded.
If it dies, then check your spelling (copy and paste the function name from functions.php into your echo [...]). It's amazing how many times I'm SURE I've spelt it right, when in fact I haven't.
If it doesn't die, check that your file is definitely called functions.php, that it's definitely inside the right theme folder for the theme you are coding.
It's possible that the functions.php file has an error in it, and so is not being parsed, hence Wordpress can't find the function. Check your logs for errors. Load the functions file and nothing else, and check that the function is working. Are you using PHP Unit or something like that?

node_delete not working

I am trying to delete some CCK nodes in Drupal using a standalone PHP script while logged in as anonymous user
if(empty($total_deals_for_this_pl)){
$node_nid = $single_result['nid'];
global $user;
$original_user = $user;
$user = user_load(1);
print $node_nid."<br>";
node_delete($node_nid);
$user = $original_user;
}
I am able to retrieve all the nid's successfully but the nodes are not getting deleted. I am loading Drupal as follows
chdir('C:\wamp\www\mysite\platform'); //my drupal resides here
require_once './includes/bootstrap.inc';
include_once './includes/common.inc';
Node_delete() has an access check for delete permissions inside it.
Test again with anonymous users given permission to delete nodes.
Also try adding
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
If that doesn't work you could try up to the session phase:
drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
and finally the full dealio:
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
Three options:
Generally, I would recommend using VBO for this kind of thing. Its a more robust solution than a custom script. It's pretty easy to set up and once you've used it you'll probably think of a dozen other ways to use it.
Failing that, make your own module and stick your custom script inside a proper hook. Your custom script on its own might not be playing along with what other modules are expecting.
If you still want to have your own separate script I suspect it's the bootstrap code that's failing. Check out drupal_bootstrap for the options available to you.

Categories