WordPress.org

Forums

How to echo adsense code in a div with PHP? (5 posts)

  1. Shrinivas
    Member
    Posted 1 year ago #

    How to echo AdSense code with PHP? Here is the sample of my code which I am working for a wordpress plugin.

    <div class='mydiv'><?php echo stripslashes($adsensecode)?></div>

    All I want to insert adsense code inside a div with PHP. I also tried with htmlentities along with stripslashes but ad in not getting displayed.

  2. byankov
    Member
    Posted 1 year ago #

    You should echo the code directly.
    If you want to escape user input in the html, you must use the htmlspecialchars php function on the adsense id for example, but not for the full code bacause it contains special characters.

  3. Shrinivas
    Member
    Posted 1 year ago #

    Thank you for your reply byankov.. May I have the example code please? I have given the text area in my plugin option page where users can add their adsense code.. I want to display that ad in a wordpress blog post.

  4. alchymyth
    Forum Moderator
    Posted 1 year ago #

    I have given the text area in my plugin option page where users can add their adsense code.

    what plugin?

    I want to display that ad in a wordpress blog post.

    do you have php code enabled in the post content, for example via a plugin?

    have you considered to add the code via a filter for 'the_content'?

    have you considered to create a shortcode for the adsense code?

  5. Shrinivas
    Member
    Posted 1 year ago #

    Dear alchymyth,

    I am creating a new plugin, added a textarea in the option page of my plugin so that users can add their adsense ad code, everything is ready, plugin receives the saved adsense code in a variable $adsensecode, finally I am using jQuery to display the same adsense advertisement inside <article>, here is the part of the code-

    jQuery("article p:eq(1)").before("<div class='mydiv'><?php echo($adsensecode); ?></div>");

    but adsense advertisement is not getting displayed. If I write some lorem ipsum text in option page, it displays but the adsense code is not outputting the advertisement.

Topic Closed

This topic has been closed to new replies.

About this Topic