Support » Developing with WordPress » Problems with Javascript – works in w3schools ‘Try it out’ but not for me!

  • Resolved mikeceret

    (@mikeceret)


    I’m trying to add a function to take a page URL, escape all special characters, and stick on the end of url for facebook, twitter etc. The URLs are taken from a site that exists and I’m trying to replicate the functionality on a replacement site – the URL given above is the equivalent page on the new site where I want to add the functionality.

    Here is what I have:

    /*  function to take URL of a page and turn it into link to post to social media
    * @param string $social_url
    * @param string $web_url
    * @return string
    *
    * Example local webpage URL = "http://amuvall.org/event/stabat-mater-de-luigi-boccherini/ "
    *
    * href for social media e.g. facebook - fixed part =  "https://www.facebook.com/sharer.php?u="
    * url to append with special characters escaped = "http%3A%2F%2Famuvall.org%2Fevent%2Fstabat-mater-de-luigi-boccherini%2F&t=Stabat%20Mater%20%20de%20Luigi%20Boccherini "
    * 
    * then concatenate the two for use in href="build_social_url(social_media_bit,local_page_url)"
    *
    */
    
    function build_social_url($social_url,$web_url)
    {
      // replace special characters in web url with & versions
      $web_url=$web_url.replace(/\:/g, "%3A");
      $web_url=$web_url.replace(/\//g, "%2F");
      $web_url=$web_url.replace(/-/g, "%20");
    
      //now concatenate with the social url
      return $social_url.concat($web_url); 
    }
    
    add_action( 'em_event_output_condition', 'build_social_url' function (10,2));

    I get errors on the each line $web_url=$web_url.replace(….
    and on the add_action line.
    I can’t find anywhere to say what the valid hooks are for add_action, but the one I’ve used is used ny plugin EVENT MANAGER, and I’ll use its shortcodes in the calls, so seems appropriate!

    On w3schools.com, the following works fine:

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph below:</p>
    
    <p id="demo">Visit http://amuvall.org/event-page/</p>
    
    <button onclick="myFunction()">Try it</button>
    
    <script>
    function myFunction() {
      var str = document.getElementById("demo").innerHTML;
      str = str.replace(/:/g, "%3A");
      str = str.replace(/-/g, "%20");
      var res = str.replace(/\//g, "%2F");
      document.getElementById("demo").innerHTML = res;
    }
    </script>
    
    </body>
    </html>
    

    Any help appreciated for a novice PHP user!

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Problems with Javascript – works in w3schools ‘Try it out’ but not for me!’ is closed to new replies.