Support

Account

Forum Replies Created

  • I’m trying to create a new toolbar to use for WYSWIYG fields, called “Editorial”. Initially I would like to only have the formatselect, bold and italic in its toolbar. I also want to remove the default toolbars.

    I’ve created it according to the documentation and the filter looks like this:

    
    add_filter( 'acf/fields/wysiwyg/toolbars' , 'kh_toolbars'  );
    function kh_toolbars( $toolbars )
    {
        // Uncomment to view format of $toolbars
        /*
        echo '< pre >';
            print_r($toolbars);
        echo '< /pre >';
        die;
        */
    
        // Add a new toolbar called "Editorial", with limited options
        $toolbars['Editorial' ] = array();
        $toolbars['Editorial' ][1] = array( 'formatselect', 'bold' , 'italic' , 'underline' );
    
        // remove the default toolbars completely
        unset( $toolbars['Basic' ] );
        unset( $toolbars['Full' ] );
    
        // return $toolbars - IMPORTANT!
        return $toolbars;
    }
    

    The toolbar is possible to select when editing the field, so I select “Editorial” there as it’s the only option after removing the default ones.

    When looking at the WYSIWYG field in the editor though, the toolbar is populated with the default buttons as well. I can also try to remove ‘italic’ for example in the array of the filter, but the italic button is still there after that.

    I would also like to know the best solution for limiting the selection of block_formats within the formatselect for a specific toolbar.

    Any idea what I’m doing wrong here?

  • Thank you! That seems to work!

    Is there any way to echo fields from the posts listed in the relationship field? My idea is to output a field called “type” that lives within the post listed. So the label in the relationship field would be:

    Post title > Type field

    To be exact, the posts listed in the relationship field is of a custom post type that has this “type” ACF field added to them.

  • Sorry to nag about this, but is there anyone who have tried something similar and can provide some example code or have the time to look through my code above and see where I’m mistaken? Would be very appreaciated.

  • Yes, I just realised this after posting the question. Thank you! 🙂

  • I realised that I didn’t explain what the problem I’m experiencing is, and I don’t want to risk that my previous post gets deleted as well if I edit it. So I’ll do an addition here instead.

    The problem is that the query simply is not changed. If I try to change a Post Object, the Post Objects that already has been selected are still available as selections in the Post Object fields.

  • Hmm. After editing my reply, it somehow got deleted. So please delete this duplicate if it somehow gets back.

    Thank you @hube2!

    Okay, bear with me on this one. I’m aware that the way I retrieve the ID and the featured image is very hackish. So I’ll try to use a more ACF way of doing this eventually. But just to demonstrate my current problem. 🙂

    I have (for now) created something in PHP that echoes the ID and the featured image URL for each post that is selected in the Post Object fields into a hidden div in the Post Object title.

    function my_post_object_result($title, $post, $field, $post_id)
    {
        $post_thumbnail_url = get_the_post_thumbnail_url($post->ID);
    
        $hidden_div = $title;
        $hidden_div .= '<div id="' . $post->ID . '" class="featured-image" style="display:none;">' . $post_thumbnail_url . ' </div>';
    
        return $hidden_div;
    }
    
    add_filter('acf/fields/post_object/result', 'my_post_object_result', 10, 4);

    The featured image URL is used for creating a separate div underneath the Post Object input with the help of JS. As they are used for a grid, it’s important for the admin to see the featured image of the post they select. The ID attribute is then used by JS to create an array containing each post ID that is currently selected, and it’s then sent as the data through AJAX.

    This is what I’m doing in JS on page load:

    $(window).on("load", function() {
    
            $(".acf-field-post-object").each(function() {
                var imageURL = $(this).find(".select2-selection").find(".featured-image").text();
                $(this).find(".acf-input").append("<div class='featured-image-container'><div class='inner'><img src='" + imageURL + "' /></div></div>");
                var newPost = $(this).find(".select2-selection").find(".featured-image").attr("id");
                selectedPosts.push(newPost);
            })
    
            var filteredPosts = selectedPosts.filter(function(id){
            	return id != undefined
            })
            var data = {
                'action': 'my_action',
                'selectedPosts': filteredPosts,
            };
    
            $.post(ajax_object.ajax_url, data, function(response) {
                console.log(response);
            });
    
        })

    The response from AJAX is returning a PHP array with all the selected posts, which is what I want. The my_action function that the AJAX is sending the data to looks like this:

    
    add_action('wp_ajax_my_action', 'my_action');
    function my_action()
    {
        $postitems = $_POST['selectedPosts'];
        print_r($postitems);
        function work_post_object_query($args, $field, $post_id)
        {
            $args['post_status'] = array('publish');
            $args['post__not_in'] = $postitems;
    
            return $args;
        }
    
        // filter for every field
        add_filter('acf/fields/post_object/query', 'work_post_object_query', 10, 3);
        exit();
    }
    

    And this is where I’m experiencing trouble. Is this the correct way of using the filter with the AJAX data? I’m suspecting (apart from the hack I used in the beginning) that I’m doing something very wrong here.

  • Thank you @hube2!

    Okay, bear with me on this one. I’m aware that the way I retrieve the ID and the featured image is very hackish. So I’ll try to use a more ACF way of doing this eventually. But just to demonstrate my current problem. 🙂

    I have (for now) created something in PHP that echoes the ID and the featured image URL for each post that is selected in the Post Object fields into a hidden div in the Post Object title.

    function my_post_object_result($title, $post, $field, $post_id)
    {
        $post_thumbnail_url = get_the_post_thumbnail_url($post->ID);
    
        $hidden_div = $title;
        $hidden_div .= '<div id="' . $post->ID . '" class="featured-image" style="display:none;">' . $post_thumbnail_url . ' </div>';
    
        return $hidden_div;
    }
    
    add_filter('acf/fields/post_object/result', 'my_post_object_result', 10, 4);

    The featured image URL is used for creating a separate div underneath the Post Object input with the help of JS. As they are used for a grid, it’s important for the admin to see the featured image of the post they select. The ID attribute is then used by JS to create an array containing each post ID that is currently selected, and it’s then sent as the data through AJAX.

    This is what I’m doing in JS on page load:

    $(window).on("load", function() {
    
            $(".acf-field-post-object").each(function() {
                var imageURL = $(this).find(".select2-selection").find(".featured-image").text();
                $(this).find(".acf-input").append("<div class='featured-image-container'><div class='inner'><img src='" + imageURL + "' /></div></div>");
                var newPost = $(this).find(".select2-selection").find(".featured-image").attr("id");
                selectedPosts.push(newPost);
            })
    
            var filteredPosts = selectedPosts.filter(function(id){
            	return id != undefined
            })
            var data = {
                'action': 'my_action',
                'selectedPosts': filteredPosts,
            };
    
            $.post(ajax_object.ajax_url, data, function(response) {
                console.log(response);
            });
    
        })

    The response from AJAX is returning a PHP array with all the selected posts, which is what I want. The my_action function that the AJAX is sending the data to looks like this:

    
    add_action('wp_ajax_my_action', 'my_action');
    function my_action()
    {
        $postitems = $_POST['selectedPosts'];
        print_r($postitems);
        function work_post_object_query($args, $field, $post_id)
        {
            $args['post_status'] = array('publish');
            $args['post__not_in'] = $postitems;
    
            return $args;
        }
    
        // filter for every field
        add_filter('acf/fields/post_object/query', 'work_post_object_query', 10, 3);
        exit();
    }
    

    And this is where I’m experiencing trouble. Is this the correct way of using the filter with the AJAX data? I’m suspecting (apart from the hack I used in the beginning) that I’m doing something very wrong here.

  • Care to share the “messy hack”?

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