【问题标题】:how to make my wordpress plugin redirect to form successful page or failed page如何使我的 wordpress 插件重定向以形成成功页面或失败页面
【发布时间】:2021-12-30 22:23:59
【问题描述】:

我创建了一个插件,可以将数据插入我的本地数据库。但是提交后,我想重定向到成功页面或不成功页面或重复记录页面。下面是我的插件代码。当点击提交时,数据库插入工作得很好,但我收到重定向页面的 404 错误。 php、apache 或 mysql 日志中也没有错误。

<?php
/*
plugin name: deano plugin
description: deano test database to insert data into books table
author: Dean-O

*/
$path = preg_replace('/wp-content.*$/', '', __DIR__);
require_once($path.'/wp-load.php');
function deanoinsertdata() {
/**
* Dean-O database insert book function
*/
global $wpdb;
if(isset($_POST['submitbtn'])){
    $data=array(
        'wp_id'=>$_POST['wp_id'],
        'title'=>$_POST['title'],
        'author'=>$_POST['author'],
    );
    $table_name = 'books';
    $foundOne = 1;

    $wp_idin = $_POST['wp_id'];
    $titlein = $_POST['title'];
    $authorin = $_POST['author'];

    /*echo ($wp_idin);
    echo ($titlein);
    echo ($authorin);
    */

    /*
    see if the record is already in the table
    */
    $sql = "select * from books"; 
    print $sql;
    $results = $wpdb->get_results($sql);
    foreach($results as $result) {
        if($result->wp_id==$wp_idin && $result->title==$titlein && $result->author==$authorin)
        {
            $foundOne = 0;
        }
    }

    if($foundOne==1) {
        $resultinsert = $wpdb->insert($table_name,$data, $format=NULL);
        if($resultinsert==1) {
             //header('Location: http://localhost/tadpolewp/deano-plugin-successful/'); 
            error_log( 'successful' );
             wp_redirect( "http://localhost/tadpolewp/deano-plugin-successful/", 301 );
            //error_log('Book saved 1');
            //echo "Book Saved 1";
        } else {
            //header('Location: http://localhost/tadpolewp/deano-plugin-failed/'); 
            error_log( 'failed to save' );
            wp_redirect( "http://localhost/tadpolewp/deano-plugin-failed/", 301 );
            //error_log('unable to save');
            //echo "Unable to Save";
        }
    } else {
        //error_log('Duplicate record found');
        //echo "Duplicate recortd found";
        //header('Location: http://localhost/tadpolewp/deano-plugin-duplicate-records/');
        error_log( 'duplicate record' );
        wp_redirect( "http://localhost/tadpolewp/deano-plugin--duplicate-records/", 301 );

    }

    

    

}
?>
<form role="form" method="post">
                <div class="form-group">
                      <?php
                // get current user ID, with default value, if empty
                $current_user_id = get_current_user_id();
            ?>

                    <input type="hidden" name="wp_id" value="<?php echo esc_attr( $current_user_id ); ?>" />
                </div>
<div class="form-group">
    <label>Book Title</label>
    <input id="title" name="title" type="text" placeholder="<?php echo esc_attr( $current_user_id ); ?>" class="form-control input-sm" required="">
</div>
<div class="form-group">
    <label>Book Author</label>
    <input id="author" name="author" type="text" placeholder="Primary Author" class="form-control input-sm" required="">
</div>
<div class="row justify-content-center">
    <div class="col-xs-4 col-sm-4 col-md-4">
        <input type="submit" value="Submit1" class="btn btn-info btn-block" name="submitbtn">
    </div>
</div>

【问题讨论】:

  • 问一个显而易见的问题,这些页面存在吗?此外,WordPress 具有重定向功能,而不是原始标头,wp_safe_redirectwp_redirect
  • 是的,页面存在。
  • 我试过了。 wp_redirect("localhost/tadpolewp/deano-plugin-successful", 301);并改变其他的指向他们的页面,仍然得到404页面。这些页面是登录用户的限制页面。插件所在的页面也是受限页面。我以普通用户身份登录,插件页面出现,数据保存到数据库,但页面重定向给我一个 404 错误
  • 所以忽略重定向和插入内容,在您刷新和/或正常访问页面时执行 404 页面?
  • 作为登录用户,我可以在没有 404 错误的情况下访问他们

标签: php wordpress plugins


【解决方案1】:

我的错...我忘记了 exit();每次重定向后。问题解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    相关资源
    最近更新 更多