0

I have below line in my return function of add_filter in wordpress.

add_filter('the_content', 'my_drama_func'); 
function my_drama_func () {
    return "function getthrough() {
        jQuery.ajax({
            url: 'ajax/ajax.php',
            type: 'POST',
            data: 'url='+url+'&un=0&pretty=1&mega=0&click=true',
            beforeSend: function(b) {..snip..},
            error: function(e) {},
            success: function(s) {}
        });
    }";
}

but in content page of view source, it is available as :-

jQuery.ajax({
    ....ajax rest code...
    //Below line is giving trouble. :(
    data: 'url='+url+'&un=0&pretty=1&mega=0&click=true',
    ...ajax rest code..
});

But my requirement is to pass it as it is with '&' and not any special character of html.

How can this be achived.

Thanks

4
  • 1
    You need to escape the characters, but this seems to be a JavaScript question not related to WordPress.
    – Wyck
    Jul 5, 2014 at 19:39
  • I am adding this code in return of add_filter.
    – John Cargo
    Jul 5, 2014 at 19:46
  • Filter doesn't output information into page source. What actually does in your case?
    – Rarst
    Jul 5, 2014 at 20:33
  • I am writing ajax function in return function of add_filter
    – John Cargo
    Jul 6, 2014 at 4:46

1 Answer 1

0

First thing is you are returning jQuery through PHP without script tag, so you should at the least be using this:

add_filter('the_content', 'my_drama_func'); 
function my_drama_func () {
    return "<script>function getthrough() {
        jQuery.ajax({
            url: 'ajax/ajax.php',
            type: 'POST',
            data: 'url='+url+'&un=0&pretty=1&mega=0&click=true',
            beforeSend: function(b) {..snip..},
            error: function(e) {},
            success: function(s) {}
        });
    }</script>";
}

I hope you understand by using the filter on the_content and not returning the original content, you are making it so any post will not show the actual content anymore, only your jQuery. If you just want the jQuery appended to the content you should at least append jQuery to original content:

add_filter('the_content', 'my_drama_func'); 
function my_drama_func ( $content ) {
    $append_jquery = "<script>function getthrough() {
        jQuery.ajax({
            url: 'ajax/ajax.php',
            type: 'POST',
            data: 'url='+url+'&un=0&pretty=1&mega=0&click=true',
            beforeSend: function(b) {..snip..},
            error: function(e) {},
            success: function(s) {}
        });
    }</script>";

    return $content . $append_jquery;
}

You can also use PHP to output the url string like so:

add_filter('the_content', 'my_drama_func'); 
function my_drama_func ( $content ) {
    $url_args = "&un=0&pretty=1&mega=0&click=true";
    $append_jquery = "<script>function getthrough() {
        jQuery.ajax({
            url: 'ajax/ajax.php',
            type: 'POST',
            data: 'url='+url+'" . $url_args . "',
            beforeSend: function(b) {..snip..},
            error: function(e) {},
            success: function(s) {}
        });
    }</script>";

    return $content . $append_jquery;
}

But this is just a fix I can give based on the limited details you have provided. What are you actually trying to do? This may (and probably isn't) the right way to do this, but it should work

5
  • i am trying to pass javascript code via return function of wordpress.
    – John Cargo
    Jul 6, 2014 at 4:49
  • Provide us a paste of your PHP code surrounding this and how you're inserting it. You can use a site like pastebin.com or even better, wpbin.io
    – sMyles
    Jul 6, 2014 at 21:54
  • I cannot paste complete code here, but i have updated look alike code in my post.
    – John Cargo
    Jul 7, 2014 at 3:19
  • Which is why I gave you a link to a site to paste the code at ( wpbin.io ) ... I updated my response
    – sMyles
    Jul 7, 2014 at 20:38
  • This may (and probably isn't) the right way to do this, but it should work, i need to insert my jquery ajax function in contet, I am writing small plugin for my voting function in content.
    – John Cargo
    Jul 9, 2014 at 3:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.