How can I initialize wpdb class in a php file? - wordpress

i am new in wordpress. i want to run a sql from a php file and this file is calling from a plugin file. my plugin file code is:
if (confirm('Are You Sure You Want to Delete?')){
window.location.href = '../wp-content/plugins/delete_data.php?id=<?php echo $db_data['dynamicmenu_id']; ?>';
} else{
}
and this code is running from script.
my delete_data.php file code is given below:
dlt_opt();
function dlt_opt(){
global $wpdb;
var_dump($wpdb);
$dlt_id = $_GET['id'];
$result = $wpdb->query( $wpdb->prepare( "DELETE FROM ".$wpdb->prefix."dynamicmenu WHERE dynamicmenu_id=".$dlt_id ) );
}
but in delete_data.php is creating error because it is not finding $wpdb and is null. So wpdb is not initializing and it can't find wpdb class. how can i add wpdb class?
Error message is:
Fatal error: Call to a member function query() on a non-object in ...\htdocs\wordpress\wp-content\plugins\delete_data.php on line 7
Need help

Better to use require_once(ABSPATH . '/wp-load.php');

I found my own answer. I need to include wp-load.php in that file.
require_once('../../wp-load.php');
although it's not a good practice to delete like this.

Related

Twig template file not recognized

I have a page-view that prints "Fields" as "unformatted list". I am trying to name my view after adding the below suggestion on the .theme file:
function mytheme_theme_suggestions_views_view_fields_alter(array &$suggestions, array $variables) {
$view_name = $variables['view']->id();
$view_display = $variables['view']->current_display;
if ($view_name && $view_display) {
$suggestions[] = $variables['theme_hook_original'] . '__' . $view_name . '__' . $view_display;
}
According to the suggestion the twig file is named as : views_view_field__my_view__page__1.html.twig
But the template is not getting recognized. View name is my_view. Can someone help on correcting my view twig template name?
You can disable the cache and enable debug mode with this instructions.
Besides, you can confirm that your param in the hook is OK:
Dont forget to rename mytheme for your real name in the function.

Getting error call_user_func_array() expects parameter 1

Currently i install wordpress in local system with jupitex theme . But after add sales price value i am getting below error in "class-wp-hook.php" file.
"call_user_func_array() expects parameter 1 to be a valid callback, function 'jupiterx_wc_product_page_custom_sale_badge' not found or invalid function name in";
frondend working fine . but in elementor backend i am getting this error.
If anyone have idea then let me know what is exact issue.
I checked the theme code the issue is with the theme. Actually they are not passing any parameters in this filter
add_filter( 'woocommerce_sale_flash', 'jupiterx_wc_product_page_custom_sale_badge' );
But on woocommerce.php template they have added $output as function parameter which doesn't exist
function jupiterx_wc_product_page_custom_sale_badge( $output ) {
$output = str_replace( 'onsale', 'onsale jupiterx-sale-badge', $output );
return $output;
}
You can ask the theme developer to resolve the issue because even if i resolve the issue it will be gone once you update the theme.
Reinstall theme to the latest version & check as well

Laravel 5 ovewrite helper function __() because used in wordpress

I have read several post on stackoverflow but didn't help, so i hope somebody can give the good answer.
I am using Laravel with wordpress. Now there is an error. Is it possible to rename or other method to change this?
Error:
Fatal error: Cannot redeclare __() (previously declared in C:app\laravel\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:821)
Laravel 5.4 added a helper function called __()
This function has the same name as WordPress's.
Just rename Laravel's __ to ___ and run "composer update".
You can find the file in:
[project folder]\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php
The problematic function is in line 947.
Don't forget to pay attention to line 938 as well.
I had this exact same issue. Couldn't do it with Bas's solution, though. Hope this helps someone.
NOTE: You shoud never change files in the Vendor folder, as they would need to be maintained. I do not recommend this solution unless you understand the consequences. Still, it gets the job done.
In de file wp-blog-header.php load before wp-load.php the l110n.php
if ( !isset($wp_did_header) ) {
$wp_did_header = true;
// Load the WordPress library.
require_once( dirname(__FILE__).'/wp-includes/l10n.php' );
require_once( dirname(__FILE__) . '/wp-load.php' );
// Set up the WordPress query.
wp();
// Load the theme template.
require_once( ABSPATH . WPINC . '/template-loader.php' );
}
And make a own helpers file in your LARAVEL project with this function with ownname:
if (! function_exists('ownname')) {
/**
* Translate the given message.
*
* #param string $key
* #param array $replace
* #param string $locale
* #return \Illuminate\Contracts\Translation\Translator|string
*/
function ownname($key = null, $replace = [], $locale = null)
{
return app('translator')->getFromJson($key, $replace, $locale);
}
}

Error 500 Call to undefined function thrive_get_theme_options() when trying to get to customize in WP

My site uninstalled all plugins so I reactivated and since then I have been greeted by Error 500 everytime I try to get to Customise (WP).
I checked out error_log and it's pointing to the funtions.php line 659 (starts 3rd line down):
function thrive_exclude_category($query)
{
$hide_cat_option = thrive_get_theme_options('hide_cats_from_blog');
if (!is_string($hide_cat_option)) {
$hide_cat_option = "";
}
$hide_categories = is_array(json_decode($hide_cat_option)) ? json_decode($hide_cat_option) : array();
$temp_query_string_part = "";
foreach ($hide_categories as $temp_cat_id) {
$temp_query_string_part .= "-" . $temp_cat_id . " ";
}
This is probably simple stuff, however, I suck. I have been lumbered with maintaining the sites and really have no say in the matter. Thanks in advance
In one of the projects I found this problem too. After study logs and debug project I find this function don't know about another function which used in theme PHP message: PHP Fatal error: Uncaught Error: Call to undefined function thrive_get_theme_options(). The main problem here in filter which running before all files will be included in theme. So function thrive_exclude_category don't find where declared thrive_get_theme_options because pre_get_posts running before we included files:
add_filter('pre_get_posts', 'thrive_exclude_category')
We need run this filter after use after_setup_theme
function run_func(){
add_filter('pre_get_posts', 'thrive_exclude_category', 999);
}
add_action('after_setup_theme', 'run_func`);
After this you not get any error and you can use your filter.
Try reactivate all plugins required by Thrive theme because of Fatal: Call to undefined function thrive_get_theme_options()

How do I call the basic WordPress header files?

I'm making a custom form action.php and currently it looks like this:
<?php
// Collect Data
$first = $_POST["first_name"];
$last = $_POST["last_name"];
$email = $_POST["email"];
$pass = $_POST["password"];
$pass2 = $_POST["confirm_password"];
$cat = $_POST["category"];
$tAndC = $_POST["terms_and_conditions"];
$privacy = $_POST["privacy_policy"];
$newsletter = $_POST["newsletter"];
die();
?>
Essentially nothing going on - the problem though is when I want to call a WordPress hook such as something like this:
$user_name = $email;
$user_id = username_exists( $user_name );
it returns an error:
Fatal error: Call to undefined function username_exists()...
I'm aware that there are probably header files I am not calling for the 'undefined function' to actually run.
I have tried adding at the top of the page: wp_head(); - but I get the following error:
Fatal error: Call to undefined function wp_head()
Include
$base_dir = dirname(dirname(__FILE__));
require_once($base_dir."/wp-load.php");
With your desired path and check
Include wp-load.php file (in the root of your wordpress installation) in your php script file like so,
require_once("/path/to/wordpress/wp-load.php");
you will have to provide the abspath of the wp-load file, now you can use all the functions of wordpress in your php script.

Resources