I keep getting a “Call to undefined function..” error displaying - php

I am new to PHP programming and I am trying to teach myself WordPress theme development for fun and I am using PhpStorm as my IDE.
I am trying to better understand the inner-workings of WordPress and I am running into a roadblock on something.
I have a sandbox plugin that I created to use to play around with WordPress.
In my “wp-content/plugins/sandbox/sandbox.php” file, I am just running basic PHP code to help me get used to the language as it relates to WordPress.
Also, I installed both Kint and Whoops using Composer to help with debugging.
Now that I got that out of the way, here is what I am doing:
Code #1
namespace MyDevPlayground\Sandbox;
add_action( 'loop_start', __NAMESPACE__ . '\process_the_string' );
function process_the_string() {
$current_user = wp_get_current_user();
$data_packet = array(
'id' => $current_user->ID,
'email' => $current_user->user_email,
'name' => array(
'first_name' => $current_user->user_firstname,
'last_name' => $current_user->user_lastname,
),
);
render_user_message( $data_packet );
}
function render_user_message( array $current_user ) {
$user_id = $current_user['id'];
d( "Welcome {$current_user['name']['first_name']}, your user id is { {$user_id} }." );
ddd( "Welcome {$current_user['name']['first_name']}, your user id is {$user_id}." );
}
When I run Code #1 above everything is fine and Kint displays the values just fine.
Now for the problem I am having that I don’t understand about WordPress:
Code #2
namespace MyDevPlayground\Sandbox;
add_action( 'loop_start', __NAMESPACE__ . '\check_logged_in_user' );
function check_logged_in_user(){
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
d('Not logged in');
} else {
ddd('Logged in');
}
}
check_logged_in_user();
When I run Code #2 above, Whoops reports the following error:
Call to undefined function MyDevPlaygroundSandbox\wp_get_current_user
For some reason when I run Code #1, the wp_get_current_user() function loads just fine, but not with Code #2.
Can someone help me understand why this is in laymen’s terms if possible?
What is the difference between Code #1 and Code #2?
How come the wp_get_current_user() function is not loading in Code #2, but it is in Code #1?
Thank you for your help.

when you use "add_action" command you can not use function name for calling that action,
you need to use the call command like this :
do_action("check_logged_in_user");
more information : https://developer.wordpress.org/reference/functions/add_action/

Related

creating a variable shortcode based on user input that returns (prechosen) variables Wordpress

I need to update values in all posts of a wordpress installation on a regular base. I was gooing to use shortcodes to insert the request into the wordpress post. Then use a custom functions.php that holds all the variables that need to be updated from time to time.
I got it working. Somehow but not the way I intended to use it. I'm a total beginner. Please consider this when answering my questions.
I want to have a function that reads what comes after honda_ and displays the correct value in wordpress without having to create a separate shortcode for each variable.
When entering [honda_link] wordpress should display the value from honda_link. When entering [honda_longlink] the value from honda_longlink variable should get displayed. I don't want to create a shortcode for each value.
I came up with this as a working solution...
// Honda
function honda() {
$honda_link = 'www.honda.com';
$honda_longlink = 'http://www.honda.com';
$honda_free = 'Free';
$honda_new = '23.688 $';
$honda_mileage = '00';
return $honda_link;
}
add_shortcode('neu', 'honda_link');
I tried some approaches by using an array but it ultimately failed all the time. I also tried it with if statements but wasn't able to get the right value displayed.
Someone willing to help a noob? I think I need to see a working example in order to understand it. The code snippets I have been looking at (that do something similiar but not the same I want to achieve) did confuse me more than they helped me.
I came up with this / Which works in a way but... This isn't very comfortable to use.
add_shortcode('HONDA','HONDA_TEST');
function HONDA_TEST($atts = array(), $content = null, $tag){
shortcode_atts(array(
'var1' => 'default var1',
'var2' => false,
'var3' => false,
'var4' => false
), $atts);
if ($atts['var2'])
return 'honda2';
else if ($atts['var3'])
return 'honda3';
else if ($atts['var4'])
return 'honda4';
else
return 'honda1';
}
So now when using:
[HONDA var1="novalue"][/HONDA]
[HONDA var2="novalue"][/HONDA]
[HONDA var3="novalue"][/HONDA]
[HONDA var4="novalue"][/HONDA]
it shows:
honda1
honda2
honda3
honda4
and so on.
Is there a better way to achieve the intended goal from post #1 ? Any way I could import the $variables from 1st post in bulk for example?
I don't have a working WP setup right now to test, but could you try this:
add_shortcode('HONDA','HONDA_TEST');
function HONDA_TEST($atts = array(), $content = null, $tag){
$atts = shortcode_atts(array(
'model' => 1,
), $atts);
$myHondas = [1 => 'honda1', 'honda2', 'honda3'];
return isset($myHondas[$atts['model']]) ? $myHondas[$atts['model']] : 'unknown model id';
}
And use it with [HONDA model="1"], [HONDA model="2"]

Clear TotalCache cache automatically from code WordPress

I have several WordPress sites hosted in my server, and some of them have the W3 Total Cache plugin installed. Right now I flush the cache for each site manually, but I want to do it programatically and all together.
I'm trying to write a PHP script that will do this. I was looking at the code used by the "Clean Cache All" option for this plugin in the dashboard, and is this piece here:
if ( $modules->plugin_is_enabled() ) {
$menu_items['10010.generic'] = array(
'id' => 'w3tc_flush_all',
'parent' => 'w3tc',
'title' => __( 'Purge All Caches', 'w3-total-cache' ),
'href' => wp_nonce_url( network_admin_url(
'admin.php?page=w3tc_dashboard&w3tc_flush_all' ),
'w3tc' ));
So I'm guessing there are 2 ways to do what I need:
1- Make a web request, but that would probably require authentication.
2- Instance the class and use the method w3tc_flush_all();
This is what I have thus far:
function clean_cache($web)
{
global $path;
$path2 = $path.$web["directory"]."/httpdocs/wp-content/plugins/w3-total-cache/";
if(file_exists($path2)) //Tienen TotalCache instalado
{
require_once $path.'w3-total-cache-api.php';
//if(class_exists("w3-total-cache-api.php"))
//{
$plugin_totalcache = & w3_instance("w3-total-cache-api.php");
//if(function_exists('w3tc_dbcache_flush'))
$plugin_totalcache->w3tc_fush_all();
//}
}
}
Any help will be much appreciated.

WHMCS: How to get the current client in addon module clientarea page?

Given that I have a WHMCS addon that I call 'my_addon'. I created the main addon file 'my_addon.php' which does contain nothing than:
<?php
function my_addon_clientarea($vars) {
$client = null;
return array(
'pagetitle' => 'My Addon',
'breadcrumb' => array('index.php?m=my_addon'=>'My Addon'),
'templatefile' => 'views/myaddon_view',
'vars' => array(
'client' => $client
)
);
}
This does basically work. It does give me my template file, everything is passed through. My question is: How do I get the currently logged in client from within that function?
I didn't find any API method and I can't see any constant which does hold this information.
There must be a way to get the current client within the clientarea? Thanks for your help!
For those who do come after me and have the same problem: it's easy to solve. Turned out, that I just had to think it through... I found the client id to be available in the $_SESSION-variable.
So, if you are looking for the client's id:
<?php
function my_addon_clientarea($vars) {
$clientid = $_SESSION['uid'];
// And so on...
}
The official way to get current user information is:
$currentUser = new \WHMCS\Authentication\CurrentUser;
$user = $currentUser->user();
You can find more information here

Wordpress Plugin in error for user Pro

Hey can anyone explain to me why I get this error when I try to save the registration form.
Fatal error: Cannot unset string offsets in /home/wolf/public_html/wp-content/plugins/userpro/admin/admin-functions.php on line 252
Here is the line in question for the php.
/** List all groups **/
function userpro_admin_list_groups(){
global $userpro;
$output = null;
$groups = $userpro->groups;
unset($groups['register']);
unset($groups['edit']);
unset($groups['view']);
unset($groups['login']);
unset($groups['social']);
$array = array(
'register' => __('Registration Fields','userpro'),
'edit' => __('Edit Profile Fields','userpro'),
'login' => __('Login Fields','userpro'),
'social' => __('Social Fields','userpro')
);
IF anyone can shed some liht on this that would be great as far as i see theres nothing written wrong, and even the plugin maker is scrating his head on this....
the config for the php is 5.4
And the Apache is 2.4
Mysql is running 5.6

MediaWiki: Show user name in skin

This should be fairly simple for anyone familiar with MediaWiki, but it's stumping me for me because being me.
I'm working on a skin, and I need to show the currently logged in user's name in a top bar - let's assume in plain text, for simplicity's sake, with changes via CSS.
Initially, I was planning on using the automatically generated one used in the personal tools bar, but since the generating line in the skin is
<?php $this->renderNavigation( 'PERSONAL' ); ?>
, it's inseparable from there. I looked in User.php and found its generation line:
public function getUserPage() {
return Title::makeTitle( NS_USER, $this->getName() );
}
So, I figure I might be able to use this function somehow, but I have very little knowledge of PHP, and am unsure how.
EDIT: It appears that this is used for the generation in the personal tools line itself, but again, I'm not sure how to adapt this.
$personal_urls['userpage'] = array(
'text' => $this->username,
'href' => &$this->userpageUrlDetails['href'],
'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
'active' => ( $this->userpageUrlDetails['href'] == $pageurl )
);
Could I duplicate this into a separate function, and make something like the following?
<?php $this->renderNavigation( 'USERNAME' ); ?>
You can use this code:
<?php echo htmlspecialchars($this->getSkin()->getUser()->getName()); ?>
Or, as the User class has a __ToString() magic method:
<?php echo htmlspecialchars($this->getSkin()->getUser()); ?>
Sources :
The SkinTemplate class in MediaWiki code documentation
The User class in the same documentation
CurrentUsers
http://www.mediawiki.org/wiki/Extension:CurrentUsers
GetUserName
http://www.mediawiki.org/wiki/Extension:GetUserName
Modify these extension for your needs
If you indeed just want the username inserted somewhere into the skin HTML, this should do it:
<?php echo htmlspecialchars( $this->username ); ?>

Categories