Wordpress插件高级自定义字段Pro不保存前端和后端

时间:2017-09-07 05:33:45

标签: php wordpress advanced-custom-fields

我正在使用带有高级自定义字段的自定义帖子类型来创建帖子的自定义条目。添加了我的海关邮政类型和选项(请在代码框上滚动查看全部):

add_action( 'init', 'register_cpt_campaigns' );

function register_cpt_campaigns() {

$labels = array(
    'name' => __( 'Campaigns', 'campaigns' ),
    'singular_name' => __( 'campaign', 'campaigns' ),
    'add_new' => __( 'Add New', 'campaigns' ),
    'add_new_item' => __( 'Add New Campaign', 'campaigns' ),
    'edit_item' => __( 'Edit Campaign', 'campaigns' ),
    'new_item' => __( 'New Campaign', 'campaigns' ),
    'view_item' => __( 'View Campaign', 'campaigns' ),
    'search_items' => __( 'Search Campaigns', 'campaigns' ),
    'not_found' => __( 'Campaign not found', 'campaigns' ),
    'not_found_in_trash' => __( 'Campaign not found', 'campaigns' ),
    'parent_item_colon' => __( 'Parent campaign:', 'campaigns' ),
    'menu_name' => __( 'Campaigns', 'campaigns' ),
);

$args = array(
    'labels' => $labels,
    'hierarchical' => false,
    'description' => 'Custom post type for Discovr Campaigns',
    'supports' => array( 'author','title' ),
    'taxonomies' => array( 'campaign_category', 'campaign_action' ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 5,
    'menu_icon' => 'dashicons-welcome-widgets-menus',
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => false,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => true,
    'capability_type' => 'post'
);

register_post_type( 'campaigns', $args );
}

自定义分类法(请在代码框上滚动查看全部):

// Register Campaign Type Taxonomy
function campaign_post_category() {

$labels = array(
    'name'                       => _x( 'Campaign Types', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Campaign Type', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Campaign Types', 'text_domain' ),
    'all_items'                  => __( 'All types', 'text_domain' ),
    'parent_item'                => __( 'Parent types', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent types:', 'text_domain' ),
    'new_item_name'              => __( 'New Campaign Type', 'text_domain' ),
    'add_new_item'               => __( 'Add New Campaign Type', 'text_domain' ),
    'edit_item'                  => __( 'Edit Campaign Type', 'text_domain' ),
    'update_item'                => __( 'Update Campaign Type', 'text_domain' ),
    'view_item'                  => __( 'View Campaign Type', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate campaign types with commas', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove campaign types', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
    'popular_items'              => __( 'Popular campaign types', 'text_domain' ),
    'search_items'               => __( 'Search campaign type', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
    'no_terms'                   => __( 'No campaign types', 'text_domain' ),
    'items_list'                 => __( 'Campaign type list', 'text_domain' ),
    'items_list_navigation'      => __( 'Campaign type list navigation', 'text_domain' ),
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => false,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => false,
    'show_tagcloud'              => false,
);
register_taxonomy( 'campaign_category', array( 'campaign_categories' ), $args );

}
add_action( 'init', 'campaign_post_category', 0 );

// Register Campaign Status Taxonomy
function campaign_post_action() {

$labels = array(
    'name'                       => _x( 'Campaign Status', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Campaign Status', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Campaign Status', 'text_domain' ),
    'all_items'                  => __( 'All Status', 'text_domain' ),
    'parent_item'                => __( 'Parent Status', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Status:', 'text_domain' ),
    'new_item_name'              => __( 'New Campaign Status', 'text_domain' ),
    'add_new_item'               => __( 'Add New Campaign Status', 'text_domain' ),
    'edit_item'                  => __( 'Edit Campaign Status', 'text_domain' ),
    'update_item'                => __( 'Update Campaign Status', 'text_domain' ),
    'view_item'                  => __( 'View Campaign Status', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate campaign status with commas', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove campaign status', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
    'popular_items'              => __( 'Popular campaign status', 'text_domain' ),
    'search_items'               => __( 'Search campaign status', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
    'no_terms'                   => __( 'No campaign status', 'text_domain' ),
    'items_list'                 => __( 'campaign status list', 'text_domain' ),
    'items_list_navigation'      => __( 'campaign status list navigation', 'text_domain' ),
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => false,
    'public'                     => true,
    'show_ui'                    => false,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => false,
    'show_tagcloud'              => false,
);
register_taxonomy( 'campaign_action', array( 'campaign_actions' ), $args );

}

add_action( 'init', 'campaign_post_action', 0 );

我还有一些函数添加到主题中以支持post-type的功能。我删除了这些以纠正我的问题,但没有解决任何问题。

// Add Default Campaign Status
function set_default_campaign_status( $post_id, $post ) {
    if ( 'publish' === $post->post_status && $post->post_type === 'campaigns' ) {
        $defaults = array(
            'campaign_action' => array( 'Draft' )
            );
        $taxonomies = get_object_taxonomies( $post->post_type );
        foreach ( (array) $taxonomies as $taxonomy ) {
            $terms = wp_get_post_terms( $post_id, $taxonomy );
            if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
                wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
            }
        }
    }
}
add_action( 'save_post', 'set_default_campaign_status', 0, 2 );

// Add Default Campaign Type

function set_default_campaign_type( $post_id, $post ) {
    if ( 'publish' === $post->post_status && $post->post_type === 'campaigns' ) {
        $defaults = array(
            'campaign_category' => array( 'Not Selected' )
            );
        $taxonomies = get_object_taxonomies( $post->post_type );
        foreach ( (array) $taxonomies as $taxonomy ) {
            $terms = wp_get_post_terms( $post_id, $taxonomy );
            if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
                wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
            }
        }
    }
}
add_action( 'save_post', 'set_default_campaign_type', 0, 2 );

// Delete post

function delete_post(){
global $post;
$deletepostlink= add_query_arg( 'frontend', 'true', get_delete_post_link( get_the_ID() ) );
if (current_user_can('edit_post', $post->ID)) {
    echo       '<a href="'.$deletepostlink.'" id=""><button class="m-t-10 m-b-10 btn btn-danger btn-cons"  type="button">Delete</button></a>';
}
}

//Redirect after delete post in frontend

add_action('trashed_post','trash_redirection_frontend');

function trash_redirection_frontend($post_id) {
if ( filter_input( INPUT_GET, 'frontend', FILTER_VALIDATE_BOOLEAN ) ) {
    wp_redirect( get_option('siteurl').'/campaigns' );
    exit;
}
}
// Add default Campaign Pages
function discovr_campaign_endpoints() {
add_rewrite_endpoint( 'overview', EP_PERMALINK );
    add_rewrite_endpoint( 'new-campaign-details', EP_PERMALINK );
    add_rewrite_endpoint( 'new-campaign-audience', EP_PERMALINK );
    add_rewrite_endpoint( 'new-campaign-page', EP_PERMALINK );
    add_rewrite_endpoint( 'new-campaign-ads', EP_PERMALINK );
add_rewrite_endpoint( 'edit-campaign', EP_PERMALINK );
add_rewrite_endpoint( 'analytics', EP_PERMALINK );
}
add_action( 'init', 'discovr_campaign_endpoints' );


// Update Campaign Page on Save
add_action( 'save_post', 'wpse105926_save_post_callback' );

function wpse105926_save_post_callback( $post_id ) {
// verify post is not a revision
if ( ! wp_is_post_revision( $post_id ) ) {

    // unhook this function to prevent infinite looping
    remove_action( 'save_post', 'wpse105926_save_post_callback' );

    // update the post slug
    wp_update_post( array(
        'ID' => $post_id,
        'post_name' => '' // do your thing here
    ));

    // re-hook this function
    add_action( 'save_post', 'wpse105926_save_post_callback' );

}
}
// Add Campaign Title Label and Instuctions
function my_acf_prepare_field( $field ) {

$field['label'] = "Campaign Title";
$field['instructions'] = "Internal use only. Ex: `Retarget Abandoned Bookings`";

return $field;

}
add_filter('acf/prepare_field/name=_post_title', 'my_acf_prepare_field');

不幸的是,每次我保存自定义字段时,它都会恢复为空或原始状态。这仅适用于ACF字段,而不会出现未使用ACF覆盖的标题,作者或分类等字段。请参阅以下gif图像以进一步了解。

Wordpress的后端,正在创建和更新帖子 Backend of post being updated

ACF自定义字段组设置 ACF Custom Fields Group Settings

Frontend Post正在创建中 Frontend Post being created

我已经尝试删除所有前端功能但仍然无法在后端工作,也删除了所有插件,而不是ACF。调试或firebug中没有错误。我只能想象字段中存在冲突,或者ACF无法找到合适的地方来保存这些内容。

非常感谢任何帮助,我希望有一些我错过的简单。

更新

这可以通过将field_namefield_label更改为隐藏的内容来实现。

ACF Field Group工作 ACF Field Group working

前端表单保存 Frontend form saving

现在让这个工作很棒,但是,我需要field_labelfield_name来匹配这些字段的用途。我可以在functions.php内覆盖这些,但这将是一场噩梦,因为我们可能要添加20个字段。

2 个答案:

答案 0 :(得分:0)

如果您将field_label更改为此广告系列的目的,并将field_name更改为what_is_the_purpose_of_this_campaign,则无效。

但是,如果你将field_label`更改为whatisthepurposeofthiscampaign而将field_name更改为whatisthepurposeofthiscampaign它确实有效。

似乎任何空格都会破坏field_label和field_name

的连接

答案 1 :(得分:0)

此错误是由某些插件的权限问题造成的。我以root身份通过FTP上传,而Wordpress使用www-data作为文件所有者。我通过控制台匹配权限,这已得到修复。

感谢您的帮助。