Support

Account

Home Forums General Issues How to turn custom fields into something I can insert into WYSIWYG editor?

Solving

How to turn custom fields into something I can insert into WYSIWYG editor?

  • I made a custom post type called “Gallery” that uses the ACF Pro repeater field. I want to be able to insert a Gallery into the middle of a blog post using the TinyMCE WordPress editor. How do I do this? Do I need to turn it into a plugin? Do I need to make a button for it?

    I don’t know if I’m even barking up the right tree here, but could someone give me a clue?

  • It’s something that you can do, but will take a lot of work as most of what you want to do is not documented and there are very few examples that exist.

    You’ll need to add custom JavaScript to ACF https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/

    In the JavaScript you’ll need to detect changes in the repeater sub fields and then you’ll need to update the value in the tinyMCE editor where you want to put the gallery field.

    Alternately you could create a custom shortcode that could be inserted into the gallery where the shortcode gets the values of the repeater and inserts a gallery into the desired location when displaying the field. https://codex.wordpress.org/Shortcode_API

  • Okay so I think the shortcode thing is probably in the right direction.

    Here’s what I’m trying right now. I created a custom post type called “Galleries”. The only field group for it is the repeater that I mentioned earlier.

    In order to insert a gallery in the middle of a blog post, my plan is to have a shortcode that injects one post (the custom Gallery Post ) into another.

    I just trie ddoing this using this plugin: https://wordpress.org/plugins/post-content-shortcodes/

    But it only seems to work for injecting The Content (from the WYSIWYG editor) and won’t pull in anything else from the php loop etc.

    Any way of doing that? Or am I going to make my own shortcode etc using the codex?

  • I think I found a better plugin. This one seems to be able to insert custom fields and stuff. I’m looking through a tutorial to see if I can get it to work. https://wordpress.org/plugins/insert-pages/

  • Okay, it does indeed work with this plugin! Except for some reason my repeater plugin is saying that it’s not finding any rows. My loop starts like this

    <section class="grid-content">
    <div class="gridGallery">
    <?php
    // check if the repeater field has rows of data
    if( have_rows('card') ):
        // loop through the rows of data
        while ( have_rows('card') ) : the_row();

    And then I have this:

    else :
     ?> <p>no rows found</p> <?php
        // no rows found
    
    endif;

    And it seems that instead of pushing out all my gallery images it’s detecting zero rows and going to the else statement. I’m sure there’s an easy fix here I’m missing but I can’t catch it exactly…

  • Okay so I found that in order to get the If and While to work I have to include the post ID number. The problem is making that dynamic…. looking that up right now.

  • I could use get_the_ID but that gets the ID for the post that I’m actually in, not for the post that I’m drawing the gallery from. I have an idea though.

  • Okay I tried my idea but for some reason it’s not working. It’s getting the post ID for the post that I’m inserting stuff into, not the custom post that I’m inserting.

  • That’s the main reason for suggesting a custom shortcode because in order to get the fields from the right post you need to tell ACF what post to get them from.

    The plugin that you mentioned does say to do exactly what you’re trying to do. I don’t know anything about that plugin, but since the instructions tell you to do what you’re doing I would ask over there.

  • I asked over at the support forums and found an answer! I just had to wrap them in an additional containing loop to the template. Full answer can be found here in case any future person searching the forums needs it:

    <?php /* Template Name: Your Custom Template Name */ ?>
    <?php while ( have_posts() ) : the_post(); ?>
      <div>Your custom content, including any template tags you want to use like <?php the_post_thumbnail(); ?></div>
    <?php endwhile; ?>

    https://wordpress.org/support/topic/problem-inserting-custom-post-into-page-has_rows-doesnt-appear-to-work/#post-9540245

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

The topic ‘How to turn custom fields into something I can insert into WYSIWYG editor?’ is closed to new replies.