Wp child theme function can't redecalre function error - php

I have used the wordpress site.
I used the child theme. After wordpress upgrade I received the following error
Fatal error: Cannot redeclare function name() (previously declared...
If I removed the function code it shows error for next function...
It shows the eror only the files which I include include("../../../wp-load.php");
I used some direct files Here I directly include the wp-load file
For these files it shows the error. If I convert these files as Template file then it working fine.
I don't know why the error occurs. Please help me.
Below is the code I used in my file
<?php
ob_start();
error_reporting(0);
include("../../../wp-load.php");
global $wpdb, $wp_hasher;
$fp_secquestion = $_REQUEST['fp_secquestion'];//'aaa';
$fp_secanswer = $_REQUEST['fp_secanswer'];//'aaa';
$fp_email = $_REQUEST['fp_email'];//'testuser5#st.com';
$user_login = sanitize_text_field($user_login);
if (empty( $fp_secquestion) || empty( $fp_secanswer) || empty( $fp_email) ) {
$log = 'validerr';
} else {
$user_data = get_user_by( 'email', trim( $fp_email ) );
if ( empty( $user_data ) )
$log = 'err';
}
echo $log;

Related

How to get the user ID in Wordpress

I have Wordpress installed in my website and I want to upload a single .php file that has to retrieve the user ID which is now logged.
This is my code of the id.php file (in the root of the website), which works:
<?php
require( dirname( __FILE__ ) . '/wp-load.php' );
$user_ID = get_current_user_id();
echo $user_ID;
?>
But I am afraid to load wp-load.php in this way. Can it cause problems in the normal work of the blog?
How can I improve my script?
If you're concerned about loading wp-load.php, instead load wp-includes/wp-user.php and use the function (shown below) defined on the WordPress developer site.
function get_current_user_id() {
if ( ! function_exists( 'wp_get_current_user' ) ) {
return 0;
}
$user = wp_get_current_user();
return ( isset( $user->ID ) ? (int) $user->ID : 0 );
}
courtesy of WordPress

first wordpress plugin with Fatal error

Trying to get a simple plugin and got error in my first steps..
<?php
/*
Plugin Name:!test
*/
require_once(includes_url() . '/pluggable.php');
function is_user_logged_in() {
$user = wp_get_current_user();
return $user->exists();
}
echo is_user_logged_in();
?>
You can't call function directly in your plugin file. Instead of it, call it inside the hook, for ex.:
function my_shortcode_func( $atts ) {
$user = wp_get_current_user();
return $user->exists();
}
add_shortcode( 'my_shortcode', 'my_shortcode_func' );

Wordpress - current_user_can in functions.php

So I am trying to run a simple if statement inside the wp-functions.php file and am using current_user_can. However I get PHP errors like: "Fatal error: Call to undefined function current_user_can() in..."
If anyone could take a look at my code, that would be much appreciated.
The code I am using is here:
global $current_user_can;
if ( current_user_can( 'manage_options' ) ) {
/* Admin User */
} else {
/* Member */
echo "<p>something</p>";
}
This usually happens because pluggable.php is not loaded by some reason. Try to add this before your function;
if(!function_exists('wp_get_current_user')) { include(ABSPATH . "wp-includes/pluggable.php"); }
It just checks if pluggable is loaded, and if not, it includes it.
if you want to check directly the role of the member, you can use this code:
global $current_user;
get_currentuserinfo();
if( !in_array( 'administrator', $current_user->roles ) ) {
//Do something
} else {
//Do something else
}
if you are creating a plugin, you must have to include like
if(!function_exists('wp_get_current_user')) { include(ABSPATH . "wp-includes/pluggable.php"); }// no need to add for theme functions
global $current_user_can;
if ( ! current_user_can( 'manage_options' ) ) {
// your stuff here
}

The plugin generated N characters of unexpected output during activation [duplicate]

This question already has answers here:
The plugin generated X characters of unexpected output during activation (WordPress)
(25 answers)
Closed 4 years ago.
I want to create a wordpress plugin by just following the example listed here based on a class OOP architecture with an external setup object, and adapting the source code on my own way like this:
main plugin file:
<?php
/*
Plugin Name: My Plugin
Description: My Plugin
Version: 1.0
Author: drwhite
Author URL: drwhite-site
Plugin URL: drwhite-site/video-ad-overlay
*/
register_activation_hook(__FILE__, array( 'VAO_Setup_File_Inc', 'on_activation'));
register_deactivation_hook(__FILE__, array( 'VAO_Setup_File_Inc', 'on_deactivation'));
register_uninstall_hook(__FILE__, array( 'VAO_Setup_File_Inc', 'on_uninstall'));
add_action('plugins_loaded', array( 'VAO_Setup_File', 'init'));
class VAO_Setup_File{
protected static $instance;
public static function init()
{
is_null( self::$instance ) AND self::$instance = new self;
return self::$instance;
}
public function __construct()
{
add_action( current_filter(), array( $this, 'load_files' ));
}
public function load_files()
{
foreach ( glob( plugin_dir_path( __FILE__ ).'includes/*.php' ) as $file ){
include_once $file;
}
}
}
In my plugin root directory i have created a subdirectory called includes within i put the setup file to be loaded on plugin load called setup.class.php:
<?php
class VAO_Setup_File_Inc
{
public static function on_activation()
{
if ( ! current_user_can( 'activate_plugins' ) )
return;
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "activate-plugin_{$plugin}" );
}
public static function on_deactivation()
{
if ( ! current_user_can( 'activate_plugins' ) )
return;
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "deactivate-plugin_{$plugin}" );
}
public static function on_uninstall()
{
if ( ! current_user_can( 'activate_plugins' ) )
return;
check_admin_referer( 'bulk-plugins' );
// Important: Check if the file is the one
// that was registered during the uninstall hook.
if ( __FILE__ != WP_UNINSTALL_PLUGIN )
return;
}
}
When i activate the plugin i got an error like the following:
I have read several questions posted by other users here and this may be duplicated question, but any of suggested answer worked for me including:
Remove space from start of tags and even remove the php end tag: nothing changed
in wp_config.php file i set wp_DEBUG to true , but it doesn't show errors
I have converted the file to UTF8 (without BOM) nothing changed
Have you put the eye in the issue ?
You are getting this error because your plugin is generating a PHP error that is being outputted to the page and causing the headers sent error you see... The problem with your code is that your function
public function load_files()
{
foreach ( glob( plugin_dir_path( __FILE__ ).'includes/*.php' ) as $file ){
include_once $file;
}
}
is not being called in time, so
register_activation_hook(__FILE__, array( 'VAO_Setup_File_Inc', 'on_activation'));
is looking for a function that doesn't exist, inside a class that doesn't exist. Move your
foreach ( glob( plugin_dir_path( __FILE__ ).'includes/*.php' ) as $file ){
include_once $file;
}
outside the class altogether, and it'll load just fine. It may require you to rethink your use of
add_action('plugins_loaded', array( 'VAO_Setup_File', 'init'));
and the way your plugin is being created, but it's a step in the right direction. If you copy and paste the code from the link you got this code from, his code displays the same problem...

PHP: __call not working properly

I'm having a problem creating a class called API_Widgets in WordPress using the magic __call function. If I simple rename the file to derp.php and the class to API_Derp then it works without problems.
The following example has been stripped of everything unimportant to this issue (so if there is any error other than the specific fatal error specified in the third code block, ignore it).
Please keep in mind that I know the core.php or API class' __call works, as renaming the widgets.php or invoking another class works just fine.
core.php:
class API {
function __call( $method, $args ) {
$rmethod = "API_{$method}";
if ( !class_exists( $rmethod ) ) {
$lmethod = strtolower( $method );
require_once( "{$lmethod}.php" );
}
return new $rmethod( $args );
}
}
widgets.php:
class API_Widgets {
private $widgets = array();
function Add( $widgets ) {
if ( is_array( $widgets ) ) {
foreach ( $widgets as $widget ) {
if ( !in_array( $widget, $this->widgets ) )
$this->widgets[] = $widget;
}
} else
$this->widgets[] = $widgets;
}
}
api.php:
$api = new API();
$widgets = $api->Widgets(); // Fatal error: Class 'API_Widgets' not found in /home4/goldencr/public_html/wp-content/plugins/minecraft-api/api/core.php on line 25
//$widgets->Add( 'some_widget' );
Extending from comment:
Though not hinted at your question, it seems you may not actually included the widgets.php. Try to use absolute path to fix that:
require_once( __DIR__.DIRECTORY_SEPARATOR.$lmethod.".php" );

Categories