-1

I'm trying to insert plugin html content to a specific spot in the frontpage of a site. The site uses the Divi theme.

I'm trying to avoid putting any code in the original Wordpress or Divi theme files for fear that any updates will wipe out the code.

This question is sort of similar to mine, however I do not think there is a hook for what I need to do.

I am able to get the plugin content to appear right at the top of the frontpage using this code in my plugin. (I've simplified the plugin code to save space in this question)

add_action( 'template_redirect', 'insert_img' );

function insert_img() {
   echo "<img src='$url'>";
}

However, it would be much more preferred to have the content display after a slider that appears at the top of the frontpage.

Ultimately, it would be great if I could have the plugin insert it's content right after this node, specifically right after the .et_pb_section comment if possible, in the DOM:

//this node contains the slider
<div class="et_pb_section et_pb_fullwidth_section  et_pb_section_0 et_section_regular">

   //lots of slider code here has been removed to save space

</div>
<!-- .et_pb_section -->

// would prefer to have plugin content appear here

//this is the next node in the DOM
<div class="et_pb_section  et_pb_section_1 et_section_regular">
    // code removed
</div>
-1

Since the DOM elements on the frontpage will never change once the site is finished, the solution I came up with, was to find the div that I wanted to follow with my plugin content using javascript, and then just append my html content to the end of div like the following:

$imageMap = "<div style='width:1080; margin:0 auto;position:relative;top:20px;margin-bottom:20px'><img src='$url'></div>";
echo '<script>
    document.addEventListener("DOMContentLoaded", function(event) {
    var el = document.getElementById("main-content").getElementsByTagName("div")[1];
    el.innerHTML += "'.$imageMap.'";
});
</script>';

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.