Announcement

Collapse
No announcement yet.

Replace text function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Replace text function

    Hi, I was trying to replace text in wordpress by adding the following to theme functions functions.php
    I was trying to strike off the normal price for hosting packages and include in the offer price.
    However, after having done so, nothing changes on my site.

    Anybody know what is wrong and how should I do it.

    Thanks.

    /*-----------------------------------------------------------------------------------*/
    /* Promotion */
    /*-----------------------------------------------------------------------------------*/


    function replace_content_on_the_fly($text){
    $replace = array(
    // 'words to find' => 'replace with this'
    '5.55' => '<strike>5.55</strike>5.00',
    '66.60' => '<strike>66.60</strike>60.00',
    '8.88' => '<strike>8.88</strike>8.00',
    '106.56' => '<strike>106.56</strike>96.00',
    '12.99' => '<strike>12.99</strike>11.00',
    '155.88' => '<strike>155.88</strike>132.00',
    '15.99' => '<strike>15.99</strike>13.00',
    '191.88' => '<strike>191.88</strike>156.00',

    );
    $text = str_replace(array_keys($replace), $replace, $text);
    return $text;
    }
    add_filter('the_content', 'replace_content_on_the_fly');
    add_filter('the_excerpt', 'replace_content_on_the_fly');



  • #2
    Did you clear your computers cache, and refresh the page a few times?

    Comment


    • #3
      It should work, but on trying it on my site, it doesn't

      PHP Code:
      '191.88' => '<strike>191.88</strike>156.00',

      ); 
      Firstly, remove the final coma, as this is not needed as this is the last array item.

      Basically "the_content" hooks into the data as it leaves the database, however the price is inserted via the RSP plugin! I think whats happening is that you are filtering too soon and the filter needs to be applied post rendering in order to "catch" the price which the RSP plugin has inserted.

      Comment


      • #4
        Wordpress doesn’t seem to have a hook to be able to capture the final output, however a hack/workaround is detailed here - http://stackoverflow.com/questions/7...al-html-output

        Comment


        • #5
          Hummmm... been playing with that and its very iffy!!! Especially if you are using other plugins to improve performance/caching !!!!!

          Maybe a client side JS/JQuery would be the best option, something like what I suggested for changing the Liquidnet address on the order form maybe?
          Last edited by clivejo; 12-06-2014, 12:08 PM.

          Comment


          • #6
            Clivejo,

            Did you two ever get something functional on this...? You can get the html from the table shortcodes and plan boxes to edit them direct and show discounts but if done the way you and ezuser are suggesting means the plugin will keep pricing updated like it should on the fly..

            Comment


            • #7
              I cant get anything to work. My last thought was to use JQuery to do a find and replace client side (like we did to replace the name and address on the order form), but no joy.

              PHP Code:
              <script>
              $(
              "body").html($("body").html().replace(/5.50/g,'<b>Found You!!</b>'));
              </
              script
              I dont really have the time to look at it, but maybe someone can get an idea of where I was going.

              Comment


              • #8
                Ok, I have actually been trying to get something similar to work for a while on this page... http://www.doneritehosting.net/website-hosting/

                *Sample code*

                BASIC FEATURES
                Disc Space|The amount of disk space indicates how much data you can upload on our servers once you purchase a low cost web hosting account with us.|=disk_space
                Monthly Traffic|The monthly traffic shows how much data you can transfer to and from the server each month. That includes uploading the websites and any other files along with all the visits of the websites hosted in this account.|=traffic
                Domain Name Registration/Transfer|<strong>Domain name registration</strong> is the service that allows you to have a unique address for your website on the Internet. Every domain name that is registered with us is automatically to point to your low cost web hosting account with us so no further configuration is required.<strong>Domain name transfer</strong> is the process of changing the company that provides the domain registration service without a change in ownership.|=domain_price

                *end code


                *This makes one row of the table*

                Disc Space|The amount of disk space indicates how much data you can upload on our servers once you purchase a low cost web hosting account with us.|=disk_space

                If you duplicate that line it will add another row to the table without doing anything else.... It would be easy to add a strikethru price above the regular monthly "Promotional" price except I have not been able to figure out how to span different pricing across the table... example if I modify......

                Disc Space|The amount of disk space indicates how much data you can upload on our servers once you purchase a low cost web hosting account with us.|=disk_space

                to say

                Regular Price| | 3.99 | 6.99 | 9.99 | |14.99

                All four prices show in every column of the table..... Have been looking for a way to make this work but its like looking for a needle in a haystack LMAO!

                Comment


                • #9
                  If the data was pulled from the database then the add_filter hook in Wordpress would work fine. But the only data being pulled from the database is just a shortcode ( ie [rsp_price_list] ), the RSP plug-in is then responsible for inserting this data. As far as I am aware there is no hook available for AFTER a plug-in has inserted its content and to do so would require a major hack to how Wordpress functions!! This hack is possible but if you have other plug-ins to increase performance there will be problems!!!
                  Last edited by clivejo; 13-06-2014, 04:45 PM.

                  Comment


                  • #10
                    Thats unfortunate too cuz most people will use a caching plugin..... keeps the pages from generating dynamic every visit... only way really to do it right is to mod the plugin and add the pricing data to the database (lightly touching it).. lot of work.. well to lose it all on the next plugin update anyhow.

                    Comment


                    • #11
                      Wait didnt read all of that...... the data in the examples above are not pulled from the database? hmmmmm.........

                      Comment


                      • #12
                        Some is, some isnt. The shortcode is [rp_plans] and it is being passed text from the database, via the shortcode to the RESP plugin, so it can be customized. ( ie best plan highlighted, custom text etc ) but the prices are still being inserted by the RSP plug-in.

                        Comment


                        • #13
                          Ok, seemed to have fixed it using a combination of answers on the internet. Please follow these instructions very careful and remember that this COULD mess up your install !!!

                          1) DON'T BLAME ME IF YOU BREAK IT !!! Do so at your own risk!!

                          2) Create a new file - /wp-content/mu-plugins/buffer.php with the following code. Note : You will have to create a folder called "mu-plugins". This is a special folder which is for Must Use plug-ins. You will probably need to do this via FTP or via the Hepsia File Manager.

                          Code:
                          <?php
                          
                          /**
                           * Output Buffering
                           *
                           * Buffers the entire WP process, capturing the final output for manipulation.
                           */
                          
                          ob_start();
                          
                          add_action('shutdown', function() {
                              $final = '';
                          
                              // We'll need to get the number of ob levels we're in, so that we can iterate over each, collecting
                              // that buffer's output into the final output.
                              $levels = count(ob_get_level());
                          
                              for ($i = 0; $i < $levels; $i++)
                              {
                                  $final .= ob_get_clean();
                              }
                          
                              // Apply any filters to the final output
                              echo apply_filters('final_output', $final);
                          }, 0);
                          3) Once uploaded to your site the above code creates a new hook called "final_output" which you can link to your custom filter. So in your functions file, modify the add_filter line to use your newly created hook. ( It would be advisable to use a child theme for this )

                          Code:
                          add_filter('final_output', 'replace_content_on_the_fly');
                          4) Make sure the replace_content_on_the_fly() is defined and contains an array of the text you want to find and replace

                          5) Your prices should now have a strike through effect. You should be able to use this method to "Find and Replace" any content on your site.
                          Last edited by clivejo; 13-06-2014, 07:23 PM.

                          Comment


                          • #14
                            sent you a pm clivejo

                            Comment


                            • #15
                              Originally posted by ezyuser View Post
                              /*-----------------------------------------------------------------------------------*/
                              /* Promotion */
                              /*-----------------------------------------------------------------------------------*/


                              function replace_content_on_the_fly($text){
                              $replace = array(
                              // 'words to find' => 'replace with this'
                              '5.55' => '<strike>5.55</strike>5.00',
                              '66.60' => '<strike>66.60</strike>60.00',
                              '8.88' => '<strike>8.88</strike>8.00',
                              '106.56' => '<strike>106.56</strike>96.00',
                              '12.99' => '<strike>12.99</strike>11.00',
                              '155.88' => '<strike>155.88</strike>132.00',
                              '15.99' => '<strike>15.99</strike>13.00',
                              '191.88' => '<strike>191.88</strike>156.00',

                              );
                              $text = str_replace(array_keys($replace), $replace, $text);
                              return $text;
                              }
                              add_filter('the_content', 'replace_content_on_the_fly');
                              add_filter('the_excerpt', 'replace_content_on_the_fly');

                              Follow my instructions in the post above and change your code to this

                              PHP Code:
                              function replace_content_on_the_fly($text){
                              $replace = array(
                              // 'words to find' => 'replace with this'
                              '5.55' => '<strike>5.55</strike> 5.00',
                              '66.60' => '<strike>66.60</strike> 60.00',
                              '8.88' => '<strike>8.88</strike> 8.00',
                              '106.56' => '<strike>106.56</strike> 96.00',
                              '12.99' => '<strike>12.99</strike> 11.00',
                              '155.88' => '<strike>155.88</strike> 132.00',
                              '15.99' => '<strike>15.99</strike> 13.00',
                              '191.88' => '<strike>191.88</strike> 156.00'
                              );
                              $text str_replace(array_keys($replace), $replace$text);
                              return 
                              $text;
                              }

                              add_filter('final_output''replace_content_on_the_fly'); 

                              Comment

                              Working...
                              X