Replace in whole wordpress page html - php

In order to optimize my website I want to replace several things in a whole HTML. I was able to replace things in the content with the following function:
function replace_text($text) {
$text = str_replace('look-for-this-string', 'replace-with-this-string', $text);
return $text;
}
add_filter('the_content', 'replace_text');
But, for example to minify the generated html I want to delete the line breaks. Is there a way of doing so in functions.php? How can I archive this?

You can either use or check how it's done in existing plugins, can't you?
For example:
WP Super Minify
This will give you not only a solution, but also you will have a nice opportunity to check how people are writing plugins. And who knows, maybe you could even contribute to this plugin?

Related

wordpress plugin that replaces the HTML/PHP code of page.php or the home page

For quite a while I am stacked with the above issue. I have done (I believe) a thorough research on the topic and tried to implement what I found, but unfortunately without any luck.
I have been working on a content-slider plugin (which now works as tandalone jquery), but in order to be able to make it as a wordpress plugin I need to add and remove various code-components from the original page.php file (or other files if the user choose a different template). I thought the best way to do this would be to "clear" the code in the existing template whatever it is and add my own after that.
I am not too familiar with php but tried to achieve this with a function which I hoped will do what I need.
In the php file of my plugin I added a function:
add_action('wp_loaded', 'remove_contents');
function remove_contents(){
include('simple_html_dom.php');
$html = file_get_html(home_url());
$dom = new DOMDocument;
$dom->loadHTML($html);
$featuredde1 = $dom->getElementById('main-content');
foreach ($featuredde1 as $node) {
$node->parentNode->removeChild($node);
}
echo $dom->saveHTML();
}
I hoped this function would remove the "main-content" div with all it's contents and leave empty space, but what happened instead is I received tons of error messages.
Again I am not so experienced with php, but could anybody advice how I might could solve this?
Thanks a lot!
I'm not sure what is inside of your #main-content area entirely, but if your target is the content part of a post or page you can create a filter on the_content in your Wordress Plugin.
https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

do_shortcode not working

I've been stuck on this for a while. I'm working on a wordpress site where I wrote the theme from scratch, I use php calls to get the wordpress functionality that I need in certain sections.
I'm trying to use a plugin, but calling it via
echo do_shortcode('[STORE-LOCATOR]');
just isnt working. Even when I switch to the default template and post that code, it still doesnt work. It simply echoes "[STORE-LOCATOR]"
Any help would be greatly appreciated.
[STORE-LOCATOR] is probably not a 'shortcode' in WordPress sense.
I encountered this on different plugin, Stream media player. They use the same syntax as shortcodes, but they are actually not.
Try using:
echo apply_filters( 'the_content',' [STORE-LOCATOR] ');
instead of do_shortcode, and see if it helps.
do_shortcode() returns a string.
I get it working by doing:
<?php echo do_shortcode(...); ?>
This is specific to the Store Locator plugin, not do_shortcode in general.
apply_filters can be an acceptable workaround for other plugins, but this does not work for Store Locator; you will only see an empty space and some controls. This is because it is looking for that shortcode in the page/post body to determine whether or not to include all of its js references at the top of the page. And without these references, nothing will work. See the sl_head_scripts function in sl-functions.php.
To change this behavior, simply modify that function to match based upon page title instead. In my instance I wanted it only on a "shop" page, so I commented out the entire $on_sl_page test and replaced it with this:
$on_sl_page = ( strpos($pagename, 'shop') === 0 );
I then called it from my page with apply_filters as indicated in the other answer:
echo apply_filters( 'the_content','[STORE-LOCATOR]');
And this appears to work perfectly.
echo do_shortcode('[STORE-LOCATOR][/STORE-LOCATOR]');
Try using shortcode after the WordPress environment has been set up.
function my_function() {
echo do_shortcode('[STORE-LOCATOR]');
}
add_action('wp', 'my_function');
If you're writing the whole thing from scratch, you'll want to make sure that the function you create is in the root php file of your plugin. The function might look something like this, but you'll have to sub in whatever logic you're using to arrive at the store location:
<?php
function doCoolStuff () {
$var1 = "value1";
$var2 = "value2";
$output = $var1+$var2;
}
return $output;
}
add_shortcode('SOTRE-LOCATIOR', 'doCoolStuff');
?>
Then in your template put the code:
<?php echo do_shortcode('[STORE-LOCATOR]');?>
Happy coding and good luck!

WordPress Custom Short Code help

Anyone familiar with WordPress shortcodes? I could really use a hand! I've inserted the following code into my functions.php file for the theme I'm using...
function create_slideshow_header($atts, $content = null){
return '<div class="item_heading">'.$content.'</div>';
}
add_shortcode('slideshow_heading', 'create_slideshow_header');
function create_slideshow_white_header($atts, $content = null){
return '<span id="dyn">'.$content.'</span>';
}
add_shortcode('slideshow_heading_white', 'create_slideshow_white_header');
function create_slideshow_content($atts, $content = null){
return '<div class="item_content">'.$content.'</div>';
}
add_shortcode('slideshow_content', 'create_slideshow_content');
Now, I was led to believe by several tutorials that this should allow me to insert the following into the text editor in the WP backend...
[slideshow_heading]SLIDESHOW HEADER[/slideshow_heading]
...and the SLIDESHOW HEADER text would be wrapped in the appropriate HTML.... but it's just displaying the above as regular text. I've cleared my cache, etc...
Is there something I'm doing wrong? Thanks in advance!
SOLUTION
I failed to mention that I was using the page.ly MultiEdit plugin--which uses "custom fields" to create extra editable regions. WordPress conveniently doesn't parse shortcodes in custom fields. Normally, you can create a filter for each custom field, but since this is a plugin, you can just edit the multiedit.php file, and change line 63 from
echo $GLOBALS['multiEditDisplay'][$index][0];
to
echo apply_filters('the_content',$GLOBALS['multiEditDisplay'][$index][0]);
With a little work, you can turn Wordpress into a truly amazing CMS!
I actually checked out your code, it works for me. Your usage is right.
Try adding a die() in there to see whether the method is called in your case.

Wordpress 3 - Remove Links from Posts via functions.php

Is there a way that I can remove links in posts via my functions.php file. Basically I don't want anyone to be able to go outside of the blog posts that are viewed. I have hundreds of posts so I obviously can't go through all of them and remove them manually. Or could I use javascript?
Thanks so much.
Updated: The jQuery below is great. Does anyone know if there is a way I can do it thru php in my functions.php file? If, for whatever ridiculous reason, someone has JS disabled is why I ask.
Thanks!
You could use JavaScript, but you're not going to be able to stop people leaving if they want to.
Something like this may work, although I haven't tested and it was written off-hand:
<script>
$('#content a').each(function() {
$(this).replaceWith($(this).text());
});
</script>
With the jQuery library, this should replace all <a> tags with what was in between them.
So Google should become just Google.
You can strip out the links on the fly using a regular expression -
$post_content = get_the_content();
$post_content = preg_replace( "|<a *href=\"(.*)\">(.*)</a>|", "\\2", $post_content );
echo $post_content
This would need to go in your theme wherever you print the_content. Untested.

the_content filter doesn't return html code, any substitute?

I was working on a WordPress plugin, and I found that when the_content filter hook is used, it gives content of the post to the function as a parameter, but in plain-text format. I want to get the HTML content of the post, any way to achieve this?
Thanks
- Kapeel
Finally, after searching a lot, I had solved it.
As said by TheDeadMedic - "Are you sure that the post content does actually contain HTML? Don't forget WordPress will add paragraphs on-the-fly, and won't necessarily store them in the DB."
WordPress does by using a function called wpautop();
I just used this with get_the_content(); , and I got it working.
Here's an example of how you can achieve this -
function myPluginReplaceContent() {
$content = wpautop(get_the_content());
$content .= myPluginGetData(); // do whatever you want to - here
return $content;
}
EDIT :
I found that this function won't apply filters by other plugins. The following function won't cause any issues.
function myPluginReplaceContent($thecontent) {
$thecontent .= myPluginGetData(); // do whatever you want to - here
return $content;
}
Do you have any plugins installed, or are you filtering the_content elsewhere?
By default, the content passed through the filter the_content is pretty much what's in the database, minus a bit of parsing (handling <!-- more --> teasers etc.).
Are you sure that the post content does actually contain HTML? Don't forget WordPress will add paragraphs on-the-fly, and won't necessarily store them in the DB.

Categories