• Hi, I wrote a code at the beginning of “functions.php” file It is being displayed first thing in the page, and I don’t want that. I want it to be displayed in a specific div. I did that with javascript, but it is not very efficient. What is a better and easy way to do this?. Please see code below and note that I want the “EDiv” to be displayed in “portalsDiv”.

    <?php
    //Display E-portals only if the current page is the home page and is user is logged in
    if (($_SERVER[‘SCRIPT_URI’] == “https://***/**/&#8221;) && is_user_logged_in()){

    echo “<div class=’EDiv’ id=’EDiv’> <h2>HEADING</h2>”;

    $user= wp_get_current_user();
    $userEmail=$user->user_email;

    $grade=$wpdb->get_var(“SELECT Grade FROM Students WHERE Email=’$userEmail'”);

    echo ‘<script type=”text/JavaScript”>
    setTimeout(function(){
    document.getElementById(“portalsDiv”).append(EDiv);
    },3000);

    </script>’ ;

    echo ‘<img src=”https://www.***.net/**/all/themes/jks/images/**.png&#8221;‘;
    echo “</div>”;
    }

    • This topic was modified 3 years, 12 months ago by hayaalahmad.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi, @hayaalahmad

    I rearranged your code a bit. Try this:

    add_action('wp_footer', 'my_custom_content');
    
    function my_custom_content() {
    if (is_home()&&is_user_logged_in()){  // or is_front_page()
    
    echo '<div class="EDiv" id="EDiv"><h2>Heading</h2>'; 
    $user= wp_get_current_user();
    $userEmail=$user->user_email;
    $grade=$wpdb->get_var("SELECT Grade FROM Students WHERE Email='$userEmail'");
    
    echo '<img src="  ">'; 
    echo '</div><script type="text/javascript">setTimeout(function(){document.getElementById("portalsDiv").append(EDiv);},3000);</script>';
    
    }
    }
    

    The key things would be

    • Use wp hook wp_footer
    • Don’t use <script> inside a div
    • I kept your approach, but you could also minimise the use of echos by using <?php ?>

    Hope this helps!

    Thread Starter hayaalahmad

    (@hayaalahmad)

    Thank you, but that did not work.
    the whole design of the page crashed.

    I just need to know to display THAT code in THAT specific div. Could you please help?

    What theme are you using?
    Can you provide a link to your site?
    The link will be public and cannot be removed later

    Thread Starter hayaalahmad

    (@hayaalahmad)

    I am using WP Student theme. Is there some kind of hook or anything to use to display that code where I want?

    Without being able to actually see your site, I don’t have any other suggestions, sadly.

    If I could see your site, maybe I could update the above suggested code in such way that it doesn’t break the design.

    The best solution is to contact your theme’s devloper or another web developer.

    Kind regards!

    • This reply was modified 3 years, 11 months ago by Vlad T.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Editing functions.php file’ is closed to new replies.