【问题标题】:How to redirect wpmu Directory "edit listing" button?如何重定向 wpmu 目录“编辑列表”按钮?
【发布时间】:2015-12-18 15:41:13
【问题描述】:

我正在使用一个 WordPress 插件,它提供了一个前端编辑器来创建自定义帖子类型(目录)。当我创建一个新目录列表时,页面重定向回 /listings/my-listings/,但是当我编辑现有列表并单击保存按钮时,它不会重定向。页面页面改为刷新并在 URL 的末尾添加一个 # (/listings/edit-listing/#)。谁能看看这个并帮助我调整 php 或提供 javascript/jquery 将我重定向回 /listings/my-listings/?

我认为这与代码底部的 .submit 类有关...

代码:

<?php
get_header(); 
/**
* The template for displaying the Add/edit listing page.
* You can override this file in your active theme.
*
* @license GNU General Public License (Version 2 - GPLv2) {@link 
http://www.gnu.org/licenses/gpl-2.0.html}
*/

global $wp_query, $wp_taxonomies, $post, $post_ID, $CustomPress_Core, 
$Directory_Core;
$listing_data   = '';
$selected_cats  = '';
$error = get_query_var('dr_error');
$post_statuses = get_post_statuses(); // get the wp post status list
$options = $Directory_Core->get_options('general');

$allowed_statuses = $Directory_Core->get_options('general'); // Get the ones we allow
$allowed_statuses['moderation'] = (empty($allowed_statuses['moderation']) ) ? array('publish' => 1,    'draft'=> 1 ) : $allowed_statuses['moderation']; // Get the ones we allow
$allowed_statuses = array_reverse(array_intersect_key($post_statuses,         $allowed_statuses['moderation']) ); //return the reduced list

//Are we adding a Listing?
if (is_page($Directory_Core->add_listing_page_id)) {
    //Make an auto-draft so we have a post id to connect attachemnts to. Set global $post_ID so media editor can hook up. Watch the case
    $post_ID = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $Directory_Core->post_type, 'post_status' => 'auto-draft' ) );
    $listing_data = get_post($post_ID, ARRAY_A );
    $listing_data['post_title'] = ''; //Have to have a title to insert the auto-save but we don't want it as final.
    $editing = false;
}

//Or are we editing a listing?
if(is_page($Directory_Core->edit_listing_page_id)){
    $listing_data = get_post(  $_REQUEST['post_id'], ARRAY_A );
    $post_ID = $listing_data['ID'];
    $editing = true;
}
$post = get_post($post_ID);

if ( isset( $_POST['listing_data'] ) ) $listing_data =    $_POST['listing_data'];

require_once(ABSPATH . 'wp-admin/includes/template.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/post.php');

$editor_settings =   array(
'wpautop' => true, // use wpautop?
'media_buttons' => true, // show insert/upload button(s)
'textarea_name' => 'listing_data[post_content]', // set the textarea name to something different, square brackets [] can be used here
'textarea_rows' => 10, //get_option('default_post_edit_rows', 10), // rows="..."
'tabindex' => '',
'editor_css' => '', // intended for extra styles for both visual and HTML editors buttons, needs to include the <style> tags, can use "scoped".
'editor_class' => 'required', // add extra class(es) to the editor textarea
'teeny' => false, // output the minimal editor config used in Press This
'dfw' => false, // replace the default fullscreen with DFW (needs specific css)
'tinymce' => true, // load TinyMCE, can be used to pass settings directly to TinyMCE using an array()
'quicktags' => true // load Quicktags, can be used to pass settings directly to Quicktags using an array()
);

$listing_content = (empty( $listing_data['post_content'] ) ) ? '' :    $listing_data['post_content'];

wp_enqueue_script('set-post-thumbnail');
?>
<script type="text/javascript" src="<?php echo $Directory_Core->plugin_url . 'ui-front/js/jquery.tagsinput.min.js'; ?>" ></script>
<script type="text/javascript" src="<?php echo $Directory_Core->plugin_url . 'ui-front/js/media-post.js'; ?>" ></script>
<script type="text/javascript" src="<?php echo $Directory_Core->plugin_url . 'ui-front/js/ui-front.js'; ?>" >
</script>

<?php if ( !empty( $error ) ): ?>
<br /><div class="error"><?php echo $error . '<br />'; ?></div>
<?php endif; ?>

<div class="dr_update_form">

    <form class="standard-form base" method="post" action="#" enctype="multipart/form-data" id="dr_update_form" >
        <input type="hidden" id="post_ID" name="listing_data[ID]" value="<?php echo ( isset( $listing_data['ID'] ) ) ? $listing_data['ID'] : ''; ?>" />
        <input type="hidden" name="post_id" value="<?php echo ( isset( $listing_data['ID'] ) ) ? $listing_data['ID'] : ''; ?>" />

        <?php if(post_type_supports('directory_listing','title') ): ?>
        <div class="editfield">
            <label for="title"><?php _e( '<strong>Agent Name:</strong>', $Directory_Core->text_domain ); ?></label>
            <input class="required" type="text" id="title" name="listing_data[post_title]" value="<?php echo ( isset( $listing_data['post_title'] ) ) ? esc_attr($listing_data['post_title']) : ''; ?>" />
            <p class="description"><?php _e( 'Enter agents name here.', $Directory_Core->text_domain ); ?></p>
        </div>
        <?php endif; ?>
<!-- Start Add Featured Image / Agent Headshot-->
        <?php if(post_type_supports('directory_listing','thumbnail') && current_theme_supports('post-thumbnails') ): ?>
        <div class="editfield">
            <div><strong>Agent Headshot:</strong></div>
            <?php if(empty($options['media_manager']) ): ?>

            <?php if(has_post_thumbnail()) the_post_thumbnail('thumbnail'); ?><br />
            <script type="text/javascript">js_translate.image_chosen = '<?php _e("Feature Image Chosen", $Directory_Core->text_domain); ?>';</script>
            <span class="upload-button">

                <?php $class = ( empty($options['field_image_req']) && !has_post_thumbnail() ) ? 'required' : ''; ?>

                <input type="file" name="feature_image" size="1" id="image" class="<?php echo $class; ?>" />
                <button type="button" class="button"><?php _e('Add Agent Headshot', $Directory_Core->text_domain); ?></button>
            </span>
            <br />

            <?php else: ?>

            <div id="postimagediv">
                <div class="inside">
                    <?php
                    $thumbnail_id = get_post_meta( $post_ID, '_thumbnail_id', true );
                    echo _wp_post_thumbnail_html($thumbnail_id, $post_ID);
                ?>
                </div>
            </div>
            <?php endif; ?>
        </div>
        <?php endif; ?>
<!-- End Add Featured Image / Agent Headshot-->
<!-- Start Taxonimy, Category and custom All Services-->
        <?php
        //get related hierarchical taxonomies
        $taxonomies = get_object_taxonomies('directory_listing', 'objects');
        $taxonomies = empty($taxonomies) ? array() : $taxonomies;

        //Loop through the taxonomies that apply
        foreach($taxonomies as $taxonomy):
        if( ! $taxonomy->hierarchical) continue;
        $tax_name = $taxonomy->name;
        $labels = $taxonomy->labels;
        //Get this Taxonomies terms
        $selected_cats = array_values( wp_get_post_terms($listing_data['ID'], $tax_name, array('fields' => 'ids') ) );


        ?>

        <div id="taxonomy-<?php echo $tax_name; ?>" class="dr_taxonomydiv">
            <label><?php echo $labels->all_items; ?></label>

            <div id="<?php echo $tax_name; ?>_all" class="dr_tax_panel">
                <?php
                $name = ( $tax_name == 'category' ) ? 'post_category' : 'tax_input[' . $tax_name . ']';
                echo "<input type='hidden' name='{$name}[]' value='0' />";          // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
            ?>
                <ul id="<?php echo $tax_name; ?>_checklist" class="list:<?php echo $labels->name; ?> categorychecklist form-no-clear">
                    <?php wp_terms_checklist( 0, array( 'taxonomy' => $tax_name, 'selected_cats' => $selected_cats, 'checked_ontop' => false ) ) ?>
                </ul>
            </div>
        </div>
        <?php endforeach; ?>
<!-- End Taxonimy, Category and custom All Services-->
        <div class="clear"><br /></div>
<!-- Start Custom Press / Custom Fields-->
        <?php if ( isset( $CustomPress_Core ) ) : ?>
            <?php echo do_shortcode('[custom_fields_input style="editfield"]'); ?>
        <?php endif; ?>

        <?php if ( !empty( $error ) ): ?>
        <br /><div class="error"><?php echo $error . '<br />'; ?></div>
        <?php endif; ?>
<!-- End Custom Press / Custom Fields-->
<!-- Start Select as Draft/Pyblic-->
        <div class="editfield" >
            <label for="title"><?php _e( 'Status', $Directory_Core->text_domain ); ?></label>
            <div id="status-box">
                <select name="listing_data[post_status]" id="listing_data[post_status]">
                    <?php
                foreach($allowed_statuses as $key => $value): ?>

                    <option value="<?php echo $key; ?>" <?php selected( ! empty($listing_data['post_status'] ) && $key == $listing_data['post_status'] ); ?> ><?php echo $value; ?></option>

                    <?php endforeach; ?>
                </select>
            </div>
            <p class="description"><?php _e( 'Please select a status for your listing. If you select your listing status as “Published” your listing will be made live and available for the public to view. If you select your listing status as “Draft” your listing will not be visible to the public and you can come back later to finish/publish.', $Directory_Core->text_domain ); ?></p>
        </div>
<!-- End Select as Draft/Pyblic-->
        <div class="submit">
            <?php wp_nonce_field( 'verify' ); ?>
            <input type="submit" value="<?php _e( 'Save Changes', $Directory_Core->text_domain ); ?>" name="update_listing">

            <input type="button" value="<?php _e( 'Cancel', $Directory_Core->text_domain ); ?>" onclick="location.href='<?php echo get_permalink($Directory_Core->my_listings_page_id); ?>'">
        </div>
        <?php //echo do_shortcode('[ct_validate]') ; ?>
    </form>
</div>

<?php get_footer(); ?>

【问题讨论】:

标签: javascript php html wordpress forms


【解决方案1】:

由于您的 PHP 在您的代码中,因此您不需要在 &lt;form&gt; 标记中使用 action 属性。将action="#" 从表单标签中取出,您应该很高兴。

就在这里:

<form class="standard-form base" method="post" action="#" enctype="multipart/form-data" id="dr_update_form" >

action 属性是将数据发送到另一个url,这就是为什么你会在当前url 的末尾得到#

这样的?

<input id="myButton" type="submit" value="<?php _e( 'Save Changes', $Directory_Core->text_domain ); ?>" name="update_listing">

<script type="text/javascript">
    document.getElementById("myButton").onclick = function () {
        location.href = "www.yoursite.com/listings/edit-listing/";
    };
</script>

【讨论】:

  • 如果该操作被删除,如果有人在/listings/edit-listing/ 上填写表单,会导致页面重定向到/listings/my-listings/
  • 嗨,亚当,那没用。现在,当我尝试单击编辑列表页面时,我被重定向到查看列表...还有其他想法吗?谢谢,威廉
  • @mevius OP 的问题是“页面页面改为刷新并在 URL 的末尾添加一个 # (/listings/edit-listing/#)”所以它在末尾添加了一个 # 而不是整个网址。去其他地方拖钓。
  • @AdamBuchananSmith 我不是在拖钓。 OP 说提交表单会在 URL AND 的末尾添加一个#,他/她希望在提交表单后重定向到/listings/my-listings/(来自/listings/edit-listing/)。您的回答负责摆脱 #,但没有其他任何作用。提高阅读理解能力。
  • 是的,好吧,继续前进!那你应该回答。
猜你喜欢
  • 2021-12-13
  • 2018-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多