如何重定向wpmu目录“编辑列表”按钮?

时间:2015-09-21 20:08:27

标签: javascript php html wordpress forms

我正在使用WordPress插件,它提供了一个前端编辑器来创建自定义帖子类型(目录)。当我创建一个新目录列表时,页面重定向回/ listing / 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(); ?>

1 个答案:

答案 0 :(得分:-1)

由于您的PHP位于代码中,因此您不需要<form>标记中的action属性。从表单标记中取出action="#",你应该很高兴。

就在这里:

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

操作属性是将数据发送到另一个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>