0

I'm trying to do something that I'm sure is very basic, but I can not figure out how to do it.

Here's the setup: I used the CPT UI plugin to create a custom post type called board_docs, and I used the Custom Fields plugin to create a custom field for the board_docs post type. The custom field allows you to add a file as an attachment. Once this file is added, a new permalink (post) is created. When I click on that permalink, the page is empty and only has a header and footer. How would I add an iframe (and other html content) to that page using php?

I tried using the loop, have_posts, in the functions.php file, but nothing is displaying. My code looks like this:

if ( have_posts() ) :
  while ( have_posts() ) : the_post();
    echo "<script>console.log('from functions.php!!!');</script>";

    ob_start(); ?>
    <div class="my-pdf-viewer">
      <p>[iframe here]</p>
    </div>
    <?php ob_get_clean();
  endwhile;
endif;

I tried using is_single, but still nothing:

if ( is_single(883) ) {// have_posts etc...}

The console.logs work and I see the logs printing to console for the specific posts, but that's it. I can't figure out how to display the html content.

I currently have a page called Board Docs where I display the permalinks for each post. When the user clicks on each link, it takes them to that empty page where I want to display html content. How would I do that using php?

UPDATE I found this handy resource a couple days ago and just wanted to share it with others who might be struggling with finding the right hook. https://adambrown.info/p/wp_hooks/hook

Also, hooks. I wish someone could've explained to me what these were when I first started. If you're doing any custom WordPress development, they're important to know. Happy coding.

  • What are you using ob_start() for? – Jacob Peattie May 1 at 0:33
  • Do you have the ability to add 'template-parts' to your theme/child theme? – Tony Djukic May 1 at 2:56
  • @TonyDjukic, yes I should be able to, although I'm not sure how to do that? Is this where I create a template-parts.php file, then "hook" it somewhere? I'm working on a child theme. The parent is Hemingway Rewritten. – sansae May 1 at 20:22
0

After searching through the wordpress docs, I found what I needed. What I was missing was the hook called "the_content". My function, and all the code I wrote inside it, was correct. I just needed this stupid hook.

function displayIframe($content) {
  //[my code to get the $currentPostID and $currentPostGuid]

  //...
  //...

  if (is_single( $currentPostID )) {// if on the specific custom post page
    $content .= "<iframe src='" . esc_html($currentPostGuid)  . "' width=100% height=900></iframe>";
  }

  return $content;
}

function main() {
  // the_content hook was what I needed; I had no idea it existed
  add_filter("the_content", __NAMESPACE__ . "\displayIframe");
}

I hope this is helpful for others. Goodluck :)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.