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

  • Resolved 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 = " "
    * href for social media e.g. facebook - fixed part =  ""
    * url to append with special characters escaped = " "
    * 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, the following works fine:

    <!DOCTYPE html>
    <p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph below:</p>
    <p id="demo">Visit</p>
    <button onclick="myFunction()">Try it</button>
    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;

    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.