How can I Check for a Text in Wordpress Post Content?

Discussion in 'Programming' started by MindReality, Jul 16, 2013.

  1. #1
    Hi,

    How can I Check for a Text in Wordpress Post Content?

    I would like to know what code can I use to do this:

    Check whether the post content contains the Text "This page has a recommended product."

    And then display a link, For E.g. "www.domain.com" under the post content.

    Is it possible to do something like that?
     
    MindReality, Jul 16, 2013 IP
  2. GuiltyCrown

    GuiltyCrown Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    Depends on the way you theme is structured because each theme has different php files usually a different one for each type of page it offers (like full width or without sidebar).

    Or you can also check the content upon creation but you could generate duplicated of domain.com at the end and you don't want that..

    $find=strpos($content,"This page has a recommended product.");

    if($find) $content.="www.domain.com";

    Something like this.
     
    GuiltyCrown, Jul 16, 2013 IP
  3. MindReality

    MindReality Well-Known Member

    Messages:
    202
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    105
    #3
    Will this work? I am trying to include a link and bold and center aligned parameters.

    
    $find=strpos($content,"[hidepost]");
     
    if($find) $content.="<div align="center"><b>Wait, this is just PART of the secrets revealed... There is MUCH vital information you are missing... <a href="http://www.mindreality.com/view.html">Click Here to View the HIDDEN PART!</a></b></div>"
    
    Code (markup):
     
    MindReality, Jul 16, 2013 IP
  4. GuiltyCrown

    GuiltyCrown Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    As long as $content is the post content yeah it will work. But [hidepost] will still remain in the content.

    If you also want to delete [hidepost] do $content=str_replace("[hidepost]","",$content);
     
    GuiltyCrown, Jul 16, 2013 IP
  5. MindReality

    MindReality Well-Known Member

    Messages:
    202
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    105
    #5
    I used my code which I mentioned above, but it is not working correctly.

    Here is a screenshot of what happens:
    http://www.mindreality.com/imagebiz/wordpress.JPG

    How can I fix it?
     
    MindReality, Jul 16, 2013 IP
  6. MindReality

    MindReality Well-Known Member

    Messages:
    202
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    105
    #6
    This is the entire part of the code in the content section:

    
    <div id="content">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <?php // Post dates off by default the_date('','<h2>','</h2>'); ?>
        <h2 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>    
        <div class="meta">Posted by <?php the_author() ?> <?php edit_post_link(__('Edit This')); ?> - <a href="http://www.mindreality.com/recform.php" target="page" onClick="window.open('','page','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=550,height=450,left=50,top=50,titlebar=yes')">Share this article with friends!</a> &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<?php if(function_exists('wp_print')) { print_link(); } ?>
    </div>
    <?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
        <div class="main">
            <?php the_content(__('(more...)')); ?>
        </div>
    <?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
    <?php
    if (!is_user_logged_in()) { ?>
    $find=strpos($content,"[hidepost]");
    if($find) $content.="<div align="center"><b>Wait, this is just PART of the secrets revealed... There is MUCH vital information you are missing... <a href="http://www.mindreality.com/view.html">Click Here to View the HIDDEN PART!</a></b></div>";
    <?php } ?>
    <p align="center"><a target="_blank" href="http://www.mindreality.com/secrets.html">Join the Free Mind Reality Newsletter and Get My Free Ebook!</a>
    <h3>Random Articles</h3>
    <?php random_posts(); ?>
    <ul>
    <li><a href="http://www.mindreality.com/archive.html">View All Articles in the Mind Reality Archive!</a></li>
    </ul>
    <?php
    if (is_user_logged_in()) { ?>
    <div align="center">
    <h3>Mind Reality Recommends</h3><br>
                  <a href="http://www.mindreality.com/special/quantum-success-secrets.html" target="_blank" rel="nofollow">
                  Quantum Success Secrets</a> - Wield Such Incredible, Reality-Bending Powers
    <br>
                  <a href="http://www.mindreality.com/special/nlp-secret.html" target="_blank" rel="nofollow">
                  The NLP Secret</a> - Instantly Change Your Life with This Technique
    <br>
                  <a href="http://www.mindreality.com/special/super-abilities.html" target="_blank" rel="nofollow">
                  Activate Your Super Abilities</a> - How to Access the Super Abilities of Your Brain
    <br>
                  <a href="http://www.mindreality.com/special/cosmic-ordering.html" target="_blank" rel="nofollow">
                  Advanced Cosmic Ordering</a> - Manifest Anything In Literally MINUTES
    </div>
    <?php } ?>
    </p>
    <?php endwhile; else: ?>
    <div class="warning">
        <p><?php _e('Page not found. <a href="http://www.mindreality.com">Return to Mind Reality</a>'); ?></p>
    </div>
    
    Code (markup):
     
    MindReality, Jul 16, 2013 IP