How can i include Guzzle into a wordpress theme or plugin. Since the theme/plugins will be distributed to non tech users i cannot ask them to do that via Composer.
I have uploaded the guzzle pack in a theme folder in libs/resources/ and in functions.php and I have defined this autoloader for guzzle classes only
spl_autoload_register( 'guzzle_autoloader' );
function guzzle_autoloader( $class_name ) {
if ( false !== strpos( $class_name, 'GuzzleHttp' ) ) {
$classes_dir = realpath( plugin_dir_path( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'libs' . DIRECTORY_SEPARATOR.'resources'. DIRECTORY_SEPARATOR .'guzzle-master'. DIRECTORY_SEPARATOR .'src'. DIRECTORY_SEPARATOR ;
$replace="GuzzleHttp\\";
$class_file = str_replace($replace,'', $class_name);
print 'request '.$classes_dir . $class_file.'</br>';
require_once $classes_dir . $class_file.'.php';
}
}
However i'm still getting this error "call to undefined function GuzzleHttp\choose_handler() "
What about composer autoload?
Simply Require Guzzle packagist repo with composer
composer require guzzlehttp/guzzle
Install
composer install
Simply autoload Guzzle Classes in your Wordpress plugin
require 'vendor/autoload.php';
//use GuzzleHttp\Client;
Related
I have a website on wordpress 5.4 and recently it was updated to version 5.5 and now i am unable to send emails using PHPMailer. The Error i get is "There has been a critical error on your website" . The below code will work perfectly for previous wordpress Versions.
include_once( ABSPATH . WPINC . '/class-phpmailer.php' );
include_once( ABSPATH . WPINC . '/includes/PHPMailer/PHPMailerAutoload.php' );
$mailserver_url = "[mailserver_url]";
$mailserver_login = "[mailserver_login]";
$mailserver_pass = '[mailserver_pass]';
$mailserver_port = '[mailserver_port]';
$email = '[email]';
$mail = new PHPMailer;
$mail->ClearAttachments();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => false
)
);
$mail->Host = $mailserver_url;
$mail->Port = $mailserver_port;
$mail->Username = $mailserver_login;
$mail->Password = $mailserver_pass;
$mail->setFrom( $email );
$mail->addReplyTo( $mailserver_login );
$mail->addAddress( $email );
$mail->Subject = 'The Subject';
$mail->isHTML();
$mail->Body = '<p>Helloo</p>';
if ( $mail->Send() ) {
echo 'sent';
}
I looked into the below article but i wasnt able to fix it https://wordpress.org/support/topic/fatal-error-after-updating-to-wp-5-5/
If you want to use it in a plugin and keep PHP Mailer compatible for older WordPress version, this is the complete solution.
global $wp_version;
if( $wp_version < '5.5') {
require_once(ABSPATH . WPINC . '/class-phpmailer.php');
require_once(ABSPATH . WPINC . '/class-smtp.php');
$mail = new PHPMailer( true );
}
else {
require_once(ABSPATH . WPINC . '/PHPMailer/PHPMailer.php');
require_once(ABSPATH . WPINC . '/PHPMailer/SMTP.php');
require_once(ABSPATH . WPINC . '/PHPMailer/Exception.php');
$mail = new PHPMailer\PHPMailer\PHPMailer( true );
}
It’s because you are attempting to load an old version of PHPMailer that no longer exists in WordPress, and getting a fatal error as a result. You should not have to load PHPMailer yourself because WordPress supplies it as standard, so refer to their docs for how to send messages, and how to create a hook to inject a custom configuration.
You can still load it yourself, but by doing so you bypass all that WP is doing for you and become liable for everything that goes with it, including loading the classes properly. To help update your code to work with PHPMailer 6.x, read the upgrade guide, or this question.
With WordPress upgrade to version 5.5 it may be necessary to include the PHPMailer SMPT.php file.
include_once (ABSPATH . WPINC . '/class-phpmailer.php');
include_once (ABSPATH . WPINC . '/PHPMailer/SMTP.php');
$mail = new PHPMailer ();
WordPress has now moved PHP mailer into a subdirecotry, you need to update your code as follows:
At the top of your function add:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
The update your code further down to:
require_once(\ABSPATH . \WPINC . "/PHPMailer/PHPMailer.php");
require_once(\ABSPATH . \WPINC . "/PHPMailer/Exception.php");
require_once(\ABSPATH . \WPINC . "/PHPMailer/SMTP.php");
$mail = new PHPMailer();
I am trying to force plugin updates with WordPress, but it doesn't seem to be working. I need to force these updates within a custom plugin itself.
I added the following filter into my plugin:
add_filter( 'auto_update_plugin', '__return_true' );
I made sure that nothing in the wp_config file disallows auto-updates. Even though I didn't need to, to be safe I also set:
define( 'FS_METHOD', 'direct' );
and define( 'WP_AUTO_UPDATE_CORE', true );
I then installed the Advanced Cron Manager plugin to trigger the wp_update_plugins event, but this did not update any plugins.
I decided to simply call the wp_maybe_auto_update() function within my plugin on init... and it worked and updated my plugin - but also disabled it!
I am wondering if anyone knows why running the wp_update_plugins cron event wouldn't be updating any plugins? There must be a really simple solution here that I'm missing! Your help would be much appreciated!
You could use the following function to update a plugin programatically:
function upgrade_plugin( $plugin_slug ) {
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader-skin.php';
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
wp_cache_flush();
$upgrader = new Plugin_Upgrader();
$upgraded = $upgrader->upgrade( $plugin_slug );
return $upgraded;
}
& you could use that in conjunction with get_plugins
// Check if get_plugins() function exists. This is required on the front end of the
// site, since it is in a file that is normally only loaded in the admin.
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$all_plugins = get_plugins();
and then loop over it to update the plugins
foreach ( $all_plugins as $key => $value ) {
upgrade_plugin( $key );
}
I found this article useful when putting together this answer: https://wpreset.com/programmatically-automatically-download-install-activate-wordpress-plugins/
From the answer above I got this to work, you just need to remove the extra / from include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader-skin.php';
to make it: include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader-skin.php';
When I am relying on a cron event (Testing with WP Crontrol plugin) to take care of plugin update, following code seems to be working with given required files:
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
include_once ABSPATH . 'wp-admin/includes/misc.php';
$upgrader = new Plugin_Upgrader();
$result = $upgrader->upgrade($plugin_slug);
if (is_wp_error($result) || !$result) {
// Check if error, else updated successfully
}
I have searched and cant find any answer.
How can I replace the themes header.php template from a custom plugin?
Everywhere I look it seems like it cant be done, and you can only change template parts from a theme/child theme.
But how does plugins like Elementor page builder do it then in their theme builder?
Thanks,
Daniel
I also needed to include a custom header.php from my plugin. Because you can register templates like page.php, post.php or archive.php from a plugin via the ${type}_template hook, I thought it shouldn't be that difficult to do the same for the header.php, but you can't load a custom header.php from directories other than parent or child theme's one.
When traversing down the get_header function you'll see the locate_template function being invoked to load the header template. But there is no way to hook into that function and to replace the path for the loaded header template:
function locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {
$located = '';
foreach ( (array) $template_names as $template_name ) {
if ( ! $template_name ) {
continue;
}
if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
$located = STYLESHEETPATH . '/' . $template_name;
break;
} elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
$located = TEMPLATEPATH . '/' . $template_name;
break;
} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
break;
}
}
if ( $load && '' !== $located ) {
load_template( $located, $require_once, $args );
}
return $located;
}
One solution to bypass this limitation would be to get rid of the get_header function within your plugin's templates and to include the plugin's header.php via require or require_once statement from within the plugin's templates. But than you will have to implement your own get_header hook and locate_template functionality to enable replacement from the parent or child theme. See this answer for more information on limitations when omitting the get_header function:
https://wordpress.stackexchange.com/a/5195/80177
I have experience using a similar page-builder to elementor called site origin.
To the best of my knowledge wordpress page-builders don't manipulate or replace the themes template files. Instead, a lot of the page-builder content is displayed in the front end with the_content() found in your template file. In addition, it appends a bunch of css and js scripts to the web page in order to handle the styling of the elements and interactivity.
As for your original question... If I understand correctly, there's no need for this plugin because the header.php file can be freely edited/replaced in the theme editor!
In the admin section, go to 'appearance' > 'editor'. Then under "theme files", select the template for header.php.
I used this solution
add_action('get_header', 'wpbet_replace_theme_header');
function wpbet_replace_theme_header(){
require plugin_dir_path( __FILE__ ) . 'templates/headers/header-padrao.php';
$templates = [];
$templates[] = 'header.php';
remove_all_actions( 'wp_head' );
ob_start();
locate_template( $templates, true );
ob_get_clean();
}
Elementor does not replace the header.php. It adopts whatever header.php the active theme has. It simply takes over the_content() part.
header.php is essential part & asset of a theme.
You may build a page template with a different header (not different header.php)
I want to compare the current version of my custom plug-in with update version of plug-in.
I get updated version of plug-in in response from server.
How can I get current version of my plug-in?
You can use get_file_data() function which is available on frontend and backend. For example:
get_file_data('/some/real/path/to/your/plugin', array('Version'), 'plugin');
if you use get_plugin_data() on the frontend it will throw an error Call to undefined function get_plugin_data(). Here is the correct way to get plugin header data.
if ( is_admin() ) {
if( ! function_exists( 'get_plugin_data' ) ) {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
$plugin_data = get_plugin_data( __FILE__ );
echo "<pre>";
print_r( $plugin_data );
echo "</pre>";
}
You could save you version in the options table for easy retrieval. But you can also use get_plugin_data for more details about a given plugin.
<?php
$data = get_plugin_data( "akismet/akismet.php", false, false );
?>
Please note that get_plugin_data is only available in the WordPress admin, it's not a front-end available function.
For users who land on this question to find out the current version of WordPress itself use this function.
// it will show only numeric value i.e 5.8.2
echo get_bloginfo( 'version' );
*Below code in display my browser directly *
<?php
/**
* Loads the WordPress environment and template.
*
* #package WordPress
*/
if ( !isset($wp_did_header) ) {
$wp_did_header = true;
require_once( dirname(__FILE__) . '/wp-load.php' );
wp();
require_once( ABSPATH . WPINC . '/template-loader.php' );
}
This code is displayed on my screen please give me any solution
It sounds like your server is not executing any PHP and just serving the plain text files.
My recommendation would be to confirm that your server has started correctly and PHP is installed and working. Can you access phpMyAdmin, cPanel or run any php through terminal?
If it's installed locally, this might help: http://reviews.cnet.co.uk/software-and-web-apps/how-to-install-and-test-wordpress-on-a-local-server-50005338/.