Cannot redeclare wp_ajax_press_this_save_post() on WordPress

So today a client’s site that had its WordPress version updated (to 4.9.1) started throwing this error when they tried to use the Metaslider plugin to upload new images. Some searching confirmed this was happening to more than a few other people but I did not easily find any actual solutions by people asking who probably like me had sites that needed the functionality this presented. The error:

1
2
3
4
PHP Fatal error:
Cannot redeclare wp_ajax_press_this_save_post()
(previously declared in wp-includes/deprecated.php:3921)
in wp-admin/includes/ajax-actions.php on line 3197

Some digging later I found out the function was not just deprecated but in 4.9.1 and above moved outside of the core entirely to an optional plugin that you can download called Press This. This is part of a general clean up initiative where non-essential functionality is being slowly moved out of a more streamlined core and moved into optional plugins.

Just installing the now optional plugin is not enough to fix the issue entirely though at least for now the easiest fix I could come up with that was the least intrusive was to comment out two functions in this file:

1
wp-admin/includes/ajax-actions.php

If you use your text editor to locate the top function, which is wp_ajax_press_this_save_post() you will find the other one below it and they should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * Ajax handler for saving a post from Press This.
 *
 * @since 4.2.0
 */
function wp_ajax_press_this_save_post() {
      include( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
      $wp_press_this = new WP_Press_This();
      $wp_press_this->save_post();
}
/**
 * Ajax handler for creating new category from Press This.
 *
 * @since 4.2.0
 */
function wp_ajax_press_this_add_category() {
      include( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
      $wp_press_this = new WP_Press_This();
      $wp_press_this->add_category();
}

Now use PHP comments to just comment out both functions entirely from being declared, so then only does the deprecation/new plugin functionality take over to make them appear like below and save the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * Ajax handler for saving a post from Press This.
 *
 * @since 4.2.0
 */
//function wp_ajax_press_this_save_post() {
//      include( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
//      $wp_press_this = new WP_Press_This();
//      $wp_press_this->save_post();
//}
/**
 * Ajax handler for creating new category from Press This.
 *
 * @since 4.2.0
 */
//function wp_ajax_press_this_add_category() {
//      include( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
//      $wp_press_this = new WP_Press_This();
//      $wp_press_this->add_category();
//}

Then your all set, hopefully WordPress have picked up on this and will just take these two declarations out in the next version or two before it gets annoying.

Woocommerce Order Notifications

Send an email if there are any woocommerce orders created a before the current Day.

$wp_query = new WP_Query(
    array(
        'posts_per_page'=> -1,
        'post_type'=>'shop_order',
        'post_status'=>'wc-processing',
        // 'post_status'=>'wc-completed',
        // 'post_status'=>'wc-pending',
        // 'post_status'=>'wc-canceled',
    )
);
$message = "These orders are curently processing<br>";
$confirm_mail_send = 0;   
foreach ($wp_query->posts as $order) {
        date_default_timezone_set('Africa/Johannesburg');
        $current_date = strtotime(date('Y-m-d'));
        // echo $current_date."<br>";
        
        $post_date = $order->post_date;
        $post_date_exploded = explode(' ', $post_date);
        $post_date_for_diff = strtotime($post_date_exploded[0]);
        // echo $post_date_for_diff."<br>";

        $diff =  $current_date - $post_date_for_diff;
        // echo $diff."<br>";
        
        function secondsToTime($seconds) {
            $dtF = new \DateTime('@0');
            $dtT = new \DateTime("@$seconds");
            return $dtF->diff($dtT)->format('%a days');
        }
        // echo secondsToTime($diff);
        
        if ($diff > 1) {
            $href = 'https://tjhokopaint.co.za/wp-admin/post.php?post='.($order->ID).'&action=edit'; 
            $message .= '<a href="'.$href.'">Order: '.$order->ID.'</a> ';
            $confirm_mail_send++;
            // echo "True";
        }
}
echo $message;
if ($confirm_mail_send != 0) {
    $to = 'garth@webfootprint.co.za';
    $subject = 'Pending Orders';
    $headers = array('Content-Type: text/html; charset=UTF-8');
    wp_mail( $to, $subject, $message, $headers);
}

 

Convert Seconds to Time
function secondsToTime($seconds) {
    $dtF = new \DateTime('@0');
    $dtT = new \DateTime("@$seconds");
    return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
}
echo secondsToTime(1640467);
Woocommerce add Custom Shipping Fee

Use your discretion and adjust the function as you need it. It should make sense when you read the function. It currently sets a shipping fee based on the total cost of the cart.

// ADD SHIPPING COST
function woo_add_cart_fee() {
    
    if (WC()->cart->cart_contents_total > 1000) {
        WC()->cart->add_fee( __('Shipping', 'woocommerce'), 100 );
    }else{
        WC()->cart->add_fee( __('Shipping', 'woocommerce'), 100 );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
PHP Start A Session and Set Session Variable
<?php 
  session_start();
  if (!isset($_SESSION['splash'])) {
    $_SESSION['splash'] = "active";
    echo "Session has been started";
  }else {
    echo "Session is currently active";
    // $_SESSION['splash'] = "active";
  }
?>

Search

Your Favourite Posts

  • Your favorites will be here.

Latest Content

© Garth Baker 2024 All rights reserved.

Pin It on Pinterest

Share This