PHP mailer not Working with wordpress 5.5 - wordpress

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();

Related

wp_handle_upload() from the backend

I've used wp_handle_upload from the front end and it works fine.
Now I want to receive base64 string (jpg) from API POST store in metabox then turn it in to jpg(until here it works fine). Then I need to upload it in the media library and attach it to a post.
when I pass the file with file_get_contents or fopen it does not work. Any ideas?
function base64ToImage($base64){
$img = base64_decode($base64);
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
// wp_handle_upload( $_POST['img'], 0 );
$fp = fopen(get_template_directory()."/xxxxxxxxxxxxxxxxxxxxxxxxx.jpg", "w+");
// write the data in image file
fwrite($fp, base64_decode($base64));
// close an open file pointer
fclose($fp);
wp_handle_upload( file_get_contents("../" ."/xxxxxxxxxxxxxxxxxxxxxxxxx.jpg"), 0 );
return 0;
}
So to my past self and to anyone that might find this useful.
I used the wp_upload_bits function instead, subsequently I used
-wp_insert_attachment
-wp_generate_attachment_metadata
-wp_update_attachment_metadata
-set_post_thumbnail in order to associate the image with a specific parent post.

SimplePie Error - Curl Timeout on Wordpress

I'm getting the error WP HTTP Error: cURL error 28: Operation timed out after 1001 milliseconds with 0 bytes received using the fetch_feed() method in my Wordpress Plugin.
This is for trying to fetch a larger RSS feed and I need to increase the Curl Timeout. Not sure why it is set to 1 second instead of 5 also?
The WP Documentation on this is not very detailed WP_Feed_Cache notably that SimplePie_Cache class documentation is not present.
Any help would be appreciated, not sure if I'm able to hook into SimplePie to increase the Curl Timeout. Also, I tried rewriting my own fetch_feed() method with no success below:
public function fetchFeed( $url ) {
if( ! class_exists('\SimplePie', false) ) {
require_once( ABSPATH . WPINC . '/class-simplepie.php' );
}
require_once( ABSPATH . WPINC . '/class-wp-feed-cache.php' );
require_once( ABSPATH . WPINC . '/class-wp-feed-cache-transient.php' );
require_once( ABSPATH . WPINC . '/class-wp-simplepie-file.php' );
require_once( ABSPATH . WPINC . '/class-wp-simplepie-sanitize-kses.php' );
$feed = new \SimplePie();
$feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
// We must manually overwrite $feed->sanitize because SimplePie's
// constructor sets it before we have a chance to set the sanitization class
$feed->sanitize = new \WP_SimplePie_Sanitize_KSES();
/* Customize sanitization */
$feed->sanitize->enable_cache = false;
$feed->sanitize->timeout = 60;
$feed->sanitize->useragent = "Custom Testing Feed Reader";
$feed->set_cache_class( 'WP_Feed_Cache' );
$feed->set_file_class( 'WP_SimplePie_File' );
$feed->set_feed_url( $url );
$feed->set_timeout( 30 );
/** This filter is documented in wp-includes/class-wp-feed-cache-transient.php */
$feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 60, $url ) ); //changing cache time to 60 seconds (instead of 12 hours)
/**
* Fires just before processing the SimplePie feed object.
*
* #since 3.0.0
*
* #param object $feed SimplePie feed object (passed by reference).
* #param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged.
*/
do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
$feed->init();
// $feed->set_output_encoding( get_option( 'blog_charset' ) );
$feed->set_output_encoding( "UTF-8" ); //set statically to UTF-8
if ( $feed->error() )
return new \WP_Error( 'simplepie-error', $feed->error() );
return $feed;
}
I was able to increase the Curl Timeout by using the following code:
//Set HTTP Request Timeout
add_filter('http_request_args', 'my_http_request_args', 100, 1);
function my_http_request_args( $r ) {
$r['timeout'] = 30;
return $r;
}
//Setting WP HTTP API Timeout
add_action('http_api_curl', 'my_http_api_curl', 100, 1);
function my_http_api_curl( $handle ) {
curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt( $handle, CURLOPT_TIMEOUT, 30 );
}
// Setting custom timeout for the HTTP request
add_filter('http_request_timeout', 'my_custom_http_request_timeout', 101 );
function my_custom_http_request_timeout( $timeLimit ) {
return 30;
}

How to use wp_extract_urls() in WordPress if I want to find NOT unique links?

How to use wp_extract_urls() in WordPress if I want to find NOT unique links? I just want to count all the links in my post!
Your best bet is likely to create a new function in your functions.php file that excludes the array_unique() part of the existing function. Something like this should work -
function wp_extract_all_urls( $content ) {
preg_match_all(
"#([\"']?)("
. "(?:([\w-]+:)?//?)"
. "[^\s()<>]+"
. "[.]"
. "(?:"
. "\([\w\d]+\)|"
. "(?:"
. "[^`!()\[\]{};:'\".,<>«»“”‘’\s]|"
. "(?:[:]\d+)?/?"
. ")+"
. ")"
. ")\\1#",
$content,
$post_links
);
$post_links = array_map( 'html_entity_decode', $post_links[2] );
return array_values( $post_links );
}

error on wordpress front-end submission file upload

I have a function working for wordpress front-end forms except for one thing. I can upload images from the front-end form but if I opt NOT to add a file, I get the following error:
Object of class WP_Error could not be converted to int in wp-includes/post.php on line 4365
Here is the code that handles the image upload part:
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file, $pid);
}
}
function insert_attachment($file_handler,$post_id,$setthumb='false') {
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
}
Note: This only happens when file is not uploaded. Seems like it's expecting an integer and throws an error if there is not file id or integer. How do I fix this? Thanks.
I discovered that when I went from my local server to a staging server, the error disappeared. Guessing that it might be a wp-config or htaccess file difference that caused the error, not sure, but anyway it's no longer an issue.

wp_enqueue_script inside shortcode [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
hi so I used a great tutorial off of http://scribu.net/wordpress/optimal-script-loading.html and I am having trouble loading the scripts when a shortcode called lightbox is used here is my code. I am at the page when it is called wanted to check with someone to see if it is properly written.
add_shortcode('lightbox', 'lightbox_handler');
function lightbox_handler($atts) {
$base = get_stylesheet_directory_uri();
wp_enqueue_script('jquery-mousewheel', $base . '/js/jquery.mousewheel-3.0.6.pack.js', array('jquery'), null, true);
wp_enqueue_script('jquery-fancybox', $base . '/js/jquery.fancybox.pack.js', array('jquery'), '1.0', true);
wp_enqueue_script('jquery-fancybox-buttons', $base . '/js/jquery.fancybox-buttons.js', array('jquery-fancybox'), '1.0', true);
wp_enqueue_script('jquery-fancybox-media', $base . '/js/jquery.fancybox-media.js', array('jquery-fancybox'), '1.0', true);
wp_enqueue_script('jquery-fancybox-thumbs', $base . '/js/jquery.fancybox-thumbs.js', array('jquery-fancybox'), '1.0', true);
wp_enqueue_style('jquery-fancybox', $base . '/css/jquery.fancybox.css', false, '1.0', true);
wp_enqueue_style('jquery-fancybox-buttons', $base . '/css/jquery.fancybox-buttons.css', array('jquery-fancybox'), '1.0', true);
wp_enqueue_style('jquery-fancybox-thumbs', $base . '/css/jquery.fancybox-thumbs.css', array('jquery-fancybox'), '1.0', true);
}
function lightbox( $atts, $content = null ) {
$defaults = apply_filters( 'toggle_shortcode_args',
array(
'title' => '',
'id' => '',
'type' => '',
'rel' => '',
'url' => '',
)
);
extract( shortcode_atts( $defaults, $atts ) );
$html .= '<a class="'.$id.'" rel="'.$rel.'" data-fancybox-group="gallery" href="'.$url.'" title="'.$title.'"><img src="'.$url.'" alt="" />';
$html .= '</a>';
return $html;
}
First point: that tutorial is based on an old version of WordPress; don't follow that one. Now:
The handle you specify in wp_register_script() must be unique. You are reusing the same handle (fancybox) for all scripts; this won't work. Give each one a different handle, e.g. 'jquery-mousewheel', 'jquery-fancybox', 'jquery-fancybox-buttons', etc. Try to stick to established naming habits that WordPress uses, see wp_enqueue_script() default scripts for examples.
If you want your scripts to be output in the footer, just say so in wp_register_script() (which you have); there is no need to use the wp_footer action. Styles should be output in the header anyway, not the footer.
You can tell WordPress to register and enqueue scripts and stylesheets for output with one call each: wp_enqueue_script() and wp_enqueue_style(). There is no need to register, then enqueue, in your simple case.
The best action hook for registering and enqueueing scripts is 'wp_enqueue_scripts'. Try to only use that one except in special circumstances.
Bonus points: where a script name already includes the version number, you can suppress the extra '?ver=nnn' that WordPress tacks on by passing null as the version string.
And finally, get_stylesheet_directory_uri() doesn't take any arguments.
NB: this gets your script working. One thing your linked tutorial does differently is only output your scripts conditionally, but there are better tutorials on that subject linked at the bottom of the wp_enqueue_scripts() manual page. First, get your site working!
class fancy{
static function init() {
add_shortcode('lightbox', array(__CLASS__, 'handle_shortcode'));
add_action('wp_enqueue_scripts', array(__CLASS__, 'wp_enqueue_scripts'));
}
static function handle_shortcode($atts) {
self::$add_script = true;
// actual shortcode handling here
}
static function wp_enqueue_scripts() {
$base = get_stylesheet_directory_uri();
wp_enqueue_script('jquery-mousewheel', $base . '/js/jquery.mousewheel-3.0.6.pack.js', array('jquery'), null, true);
wp_enqueue_script('jquery-fancybox', $base . '/js/jquery.fancybox.pack.js', array('jquery'), '1.0', true);
wp_enqueue_script('jquery-fancybox-buttons', $base . '/js/jquery.fancybox-buttons.js', array('jquery-fancybox'), '1.0', true);
wp_enqueue_script('jquery-fancybox-media', $base . '/js/jquery.fancybox-media.js', array('jquery-fancybox'), '1.0', true);
wp_enqueue_script('jquery-fancybox-thumbs', $base . '/js/jquery.fancybox-thumbs.js', array('jquery-fancybox'), '1.0', true);
wp_enqueue_style('jquery-fancybox', $base . '/css/jquery.fancybox.css', false, '1.0', true);
wp_enqueue_style('jquery-fancybox-buttons', $base . '/css/jquery.fancybox-buttons.css', array('jquery-fancybox'), '1.0', true);
wp_enqueue_style('jquery-fancybox-thumbs', $base . '/css/jquery.fancybox-thumbs.css', array('jquery-fancybox'), '1.0', true);
}
}
fancy::init();
PS: I suspect that jquery.fancybox.pack.js is a packed (i.e. minified) version of jquery.fancybox.js, so use that one as 'jquery-fancybox' if it is; you don't need both.
The first argument to wp_register_script needs to be unique for each script (see the codex), so you might need names like "fancybox.mousewheel", "fancybox", "fancybox.buttons" etc to differentiate. I'd check, but I suspect you only need one of fancybox.js and fancybox.pack.js - I'd think the pack file is a minified version of fancybox.js
But what version of WordPress are you using? The link at the top of the tutorial you mentioned shows a cleaner way to do it in WordPress 3.3+, removing the need for the class.

Resources