Support

Account

Home Forums General Issues Not saving Frontend and Backend

Solving

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:

    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:

    // 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. 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: <code>Retarget Abandoned Bookings</code>";
    
    return $field;
    
    }
    add_filter('acf/prepare_field/name=_post_title', 'my_acf_prepare_field');

    Unfortunately, every time 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. Please see the following gif images to understand further.

    Backend of WordPress with Post being created and updated
    null

    ACF Custom Fields Group Settings

    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. No error in debug or 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.

    Any help is greatly appreciated and I hope that there is something simple that I have missed.

    UPDATE
    This works by changing the field_name and field_label to something obscure.

    ACF Field Group working

    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. I could overwrite these within the functions.php but this will be a nightmare as we potentially have 20 fields to add.

  • If I 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. However, if I change the field_label to whatisthepurposeofthiscampaign and the field_name to
    whatisthepurposeofthiscampaign it does work. It seems that any spaces break the connection of the field_label and field_name

  • My conclusion is that unless the field_name and field_label are the same it does not work. The same is said for spaces between words of each. Your help is muchly appreciated.

  • Not sure where your problem in in your last comment. There is not relation between the field label and the field name that would cause things not to work if they do not match.

    It seems from your comments that you’ve got the initial problem fixed. Can you explain more clearly what the problem is that you’re now having?

  • Thank you, John but that is exactly what I am saying. If the field_label and field_name do not match they do not save the input.

    To save the input, I must match the field_label and field_name, for ex. Field label = Example Field and Field Name = example_field will not save the input. However, Field label = examplefield and Field Name = examplefield will save the input.

  • This is not how it’s supposed to be working, and it is not how it is working for me on a test site. The only thing that I can think of is that you have something that’s interfering with the correct operation of ACF, like another plugin. You’ll need to figure out where the problem is by deactivating other plugins and maybe changing themes.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Not saving Frontend and Backend’ is closed to new replies.