简体   繁体   English

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

[英]Wordpress plugin Advanced Custom Fields Pro not saving Frontend and Backend

I am using A custom Post Type with Advanced Custom Fields to create custom entries of a post. 我正在使用带有高级自定义字段的自定义帖子类型来创建帖子的自定义条目。 My Customs Post Type and Options are added with (please scroll on codebox to see all): 添加了我的海关邮政类型和选项(请在代码框上滚动查看全部):

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

And custom Taxonomies with (please scroll on codebox to see all): 和自定义分类法(请在代码框上滚动查看全部):

// 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 );

I also have some functions that are added to the theme to support my functionality of the post-type . 我还有一些函数添加到主题中以支持我的post-type I have removed these to rectify my issue but did not fix anything. 我删除了这些以纠正我的问题,但没有解决任何问题。

// 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');

Unfortunately, everytime I save a custom field it reverts to empty or as it's original. 不幸的是,每次我保存自定义字段时,它都会恢复为空或原始状态。 This only happens with the ACF fields and not with fields such as title, author or taxonomies not overwritten with ACF. 这仅适用于ACF字段,而不会出现未使用ACF覆盖的标题,作者或分类等字段。 Please see the following gif images to understand further. 请参阅以下gif图像以进一步了解。

Backend of Wordpress with Post being created and updated Wordpress的后端与Post正在创建和更新 更新帖子的后端

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

Frontend Post being created 前端邮政被创建 前端邮政被创建

I have tried removing all frontend funtionality but still doesn't work in the backend, also removed all plugins either than ACF. 我已经尝试删除所有前端功能但仍然无法在后端工作,也删除了所有插件比ACF。 No error in debug or firebug. 调试或firebug中没有错误。 I can only imaging that there is a conflict in the fields, or ACF is not able to find the appropriate place to save these. 我只能想象字段中存在冲突,或者ACF无法找到保存这些冲突的适当位置。

Any help is greatly appreciated and I hope that there is something simple that I have missed. 非常感谢任何帮助,我希望有一些我错过的简单。

UPDATE UPDATE

This works by changing the field_name and field_label to something obscure. 这可以通过将field_namefield_label更改为field_label来实现。

ACF Field Group working ACF Field Group工作 ACF Field Group工作

Frontend form saving 前端形式保存 前端形式保存

Getting this to now work is great, however, I need the field_label and field_name to match the purpose of these fields. 现在field_label工作很好,但是,我需要field_labelfield_name来匹配这些字段的用途。 I could overwrite these within the functions.php but this will be a nightmare as we potentially have 20 fields to add. 我可以在functions.php覆盖这些,但这将是一场噩梦,因为我们可能有20个字段要添加。

If you change the field_label to what is the purpose of this campaign and the field_name to what_is_the_purpose_of_this_campaign it does not work. 如果您将field_label更改为此广告系列的目的,并将field_name更改为what_is_the_purpose_of_this_campaign它不起作用。

However, if you change the field_label` to whatisthepurposeofthiscampaign and the field_name to whatisthepurposeofthiscampaign it does work. 但是,如果你将field_label`更改为whatisthepurposeofthiscampaign而将field_name更改为whatisthepurposeofthiscampaign它确实有效。

It seems that any spaces break the connection of the field_label and field_name 似乎任何空格都会破坏field_label和field_name的连接

This error was created by permissions problems with certain plugins. 此错误是由某些插件的权限问题创建的。 I had uploaded via FTP as root, whereas Wordpress was using www-data as the file owner. 我以root身份通过FTP上传,而Wordpress使用www-data作为文件所有者。 I matched the permissions through the console and this has been fixed. 我通过控制台匹配权限,这已得到修复。

Thank you for your help. 谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM