Home Forums Backend Issues (wp-admin) create post within acf block Reply To: create post within acf block

  • I don’t think it’s worth the effort but in theory it may be possible..

    1. Extend ACF Block with a JavaScript Interface
    You would need to enhance your ACF block with JavaScript (likely using React if you are using the latest WordPress editor, Gutenberg) to include a user interface element, such as a button, for adding new ‘cassette_card’ posts.

    2. Use WordPress REST API
    Leverage the WordPress REST API to create new posts. You’d need to ensure that your custom post type ‘cassette_card’ is exposed to the REST API with the appropriate arguments set in the register_post_type() function, like so:

    register_post_type('cassette_card', [
        'public' => true,
        'show_in_rest' => true,
        // Other necessary arguments...

    3. JavaScript to Handle Post Creation
    Within your block’s JavaScript, add a function that handles the creation of new ‘cassette_card’ posts. This function would use the WordPress REST API to create a new post asynchronously. Here’s a basic example using fetch API:

    function createCassetteCard(title, content) {
        const postData = {
            title: title,
            content: content,
            status: 'publish',  // Or 'draft' depending on your needs
        fetch('/wp-json/wp/v2/cassette_card', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-WP-Nonce': wpApiSettings.nonce  // nonce for verification
            body: JSON.stringify(postData)
        .then(response => response.json())
        .then(post => {
            console.log('New cassette card created:', post);
            // Handle the newly created post (e.g., adding to a select dropdown)
        .catch(error => console.error('Error:', error));

    4. Authentication and Security
    Ensure that proper authentication (like nonce checks) and capability checks are implemented to secure the post creation process. The X-WP-Nonce header in the fetch API call above is crucial for security.

    5. Update the Editor Interface
    After a post is successfully created, you’ll need to update the editor’s interface to include the new post. This might mean refreshing a dropdown or a list component that lets you pick ‘cassette_card’ posts.

    6. Enqueue Scripts and Localize
    Make sure your JavaScript is properly enqueued and localized in your plugin or theme’s functions file to ensure it has access to WordPress core functionality like wpApiSettings.nonce.

    None of this is tested and may not even work. Maybe this will help though.