【问题标题】:Redirect doesn't work on a custom plugin wordpress重定向不适用于自定义插件 wordpress
【发布时间】:2020-06-02 08:43:35
【问题描述】:

当文件上传到媒体库以裁剪/调整大小时,我正在尝试重定向。 我正在添加一个自定义模板,并在管理员将媒体添加到库(管理面板)后尝试重定向。 但是当我上传媒体时,我在 wordpress 中遇到错误:“上传过程中发生错误。请稍后再试。” 什么都没有。 我的文件已上传并保存,但未显示在媒体库中。 我浏览了许多主题,但没有找到答案。也许我不能在插件中那样重定向。

我在 wp-contents/plugins/myplugin/plugin.php 和 custom-template.php 中有两个 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();
?>

还有我的自定义模板:

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

Network console log

很确定我做错了,但我是 wordpress 的新手,我对任何有建设性的 cmets 持开放态度。 如果您需要更多信息,请询问。提前谢谢你。

【问题讨论】:

  • redirect_to_crop 函数的视图是否出现?

标签: php wordpress plugins


【解决方案1】:

似乎我不能那样做,所以我所做的就是用 jQuery 来做。 我正在我的媒体库上添加一个按钮并隐藏已经存在的那个。

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

我可以在使用croppie将文件上传到服务器之前对其进行修改。

我希望我能帮助别人。

【讨论】:

    猜你喜欢
    • 2017-04-14
    • 1970-01-01
    • 2020-04-18
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多