简体   繁体   中英

Redirect doesn't work on a custom plugin wordpress

I'm trying to redirect when a file has been uploaded in media library to crop/resize it. I'm adding a custom template and try to redirect after the admin add a media to library (admin panel). But when I upload media I've got an error in wordpress: "An error occurred during upload. Please try again later." And nothing comes. My file is uploaded and saved but doesn't show in media library. I've traveled through many topics but didn't find an answer. Maybe I can't redirect like that in a plugin.

I've got two php file in wp-contents/plugins/myplugin/plugin.php and custom-template.php

<?php
/*
Plugin name: Waouh_pictures
Description: 
Version: 1.0
Author: Waouh
*/
require_once("lib/shortpixel-php-req.php");

if(!defined('ABSPATH'))
exit;

class plugin{
  public function __construct(){

    // Some code here

    /* Add cropping template to wordpress */
    function page_template( $page_template )
    {
      if ( is_page( 'cropping-waouh' ) ) {
        $page_template = dirname( __FILE__ ) . '/crop_waouh.php';
      }
      return $page_template;
    }

    add_filter( 'page_template', 'page_template' );

    /* Redirect to cropping-waouh when a file is uploaded */
    function redirect_to_crop( $upload ) {
      $url = get_site_url() . "/cropping-waouh";
      wp_redirect( $url );
      exit;
      return $upload;
    }

    add_filter( 'wp_handle_upload', 'redirect_to_crop' );
  }
}
new plugin();
?>

And my custom template:

<?php
/* Template Name:  Cropping Waouh */
echo 'This is my cropping page :)';
?>

Network console log

Pretty sure I'm doing wrong but I'm new to wordpress and I'm open to any constructive comments. If you need more informations just ask. Thank you in advance.

It seems like I can't do it that way so the thing I've done was to do it with jQuery. I'm adding a button on my media library and hide the one who already exists.

jQuery(document).ready(function($){
  $("#wp-media-grid > a").after("<form action=\"/wp-content/plugins/waouh_pictures/function.php\"><input type=\"file\" id=\"button_upload_waouh\" name=\"filename\" method=\"POST\"><input type=\"submit\" value=\"Ajouter\"></form>");
  $("#wp-media-grid > a").hide();
});

And I can modify the file before uploading it into the server using croppie.

I hope I could help someone with that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM