code suggestion

Home Forums Legacy Support 4.0 Bugs & Issues code suggestion

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #361311
    Ernesto Lee
    Participant
    how can i run this code only on the single course page?? <script> jQuery(window).load(function($){ let enrollNowButton = document.querySelector(".single-course .enrol_in_batch.button.is-primary.small"); let takeThisCourse = document.querySelector('.single-course .course_button.button.full'); if(enrollNowButton.textContent == "Enrol Now" && takeThisCourse.textContent.includes("Take this course") && document.querySelector("body").classList.contains("logged-in")){ takeThisCourse.style.display="none"; jQuery('.single-course .course_pricing>strong').append("Click the Enroll Now button to subscribe in this course."); } }); </script> please suggest me hooks and actions
    #361529
    Anshuman Sahu
    Keymaster
    Maybe try this. :
    <script>
    jQuery(window).load(function($){
    let enrollNowButton = document.querySelector(".single-course .enrol_in_batch.button.is-primary.small");
    let takeThisCourse = document.querySelector('.single-course .course_button.button.full');
    if(enrollNowButton && enrollNowButton.innerText == "Enrol Now" && takeThisCourse && takeThisCourse.innerText.includes("Take this course") && document.querySelector("body").classList.contains("logged-in")){
    takeThisCourse.style.display="none";
    jQuery('.single-course .course_pricing>strong').append("Click the Enroll Now button to subscribe in this course.");}
    });
    </script>
    #361590
    Ernesto Lee
    Participant
    i want to run this code only on single course page. the issue is if i put this code in wplms >> footer >> google analytic code it loads on every page. because enrollNowButton && takeThisCourse can be find only on single course page. these 2 variable gives error on all other pages because this variable can be find in dom only on single course page. so an action hook is required so that it only runs in the single course page footer
    #361777
    Anshuman Sahu
    Keymaster
    in that case please add this code in your wplms-customizer.php file in wplms-customizer plugin :
    
    add_action('wp_footer',function(){
    if(is_singular('course') || is_singular('course-layout')){
    ?>
    <script>
    jQuery(window).load(function($){
    let enrollNowButton = document.querySelector(".single-course .enrol_in_batch.button.is-primary.small");
    let takeThisCourse = document.querySelector('.single-course .course_button.button.full');
    if(enrollNowButton && enrollNowButton.innerText == "Enrol Now" && takeThisCourse && takeThisCourse.innerText.includes("Take this course") && document.querySelector("body").classList.contains("logged-in")){
    takeThisCourse.style.display="none";
    jQuery('.single-course .course_pricing>strong').append("Click the Enroll Now button to subscribe in this course.");}
    });
    </script>
    <?php
    }
    });
    
    #362084
    Ernesto Lee
    Participant
    thanks it worked.
    #362111
    Ada
    Participant
    Thank-you for confirming. closing this topic.
Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘code suggestion’ is closed to new replies.