Wordpress 3 - Remove Links from Posts via functions.php - 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.

Related

Using shortcode in HREF in content loaded via functions.PHP

Wordpress | Advanced Custom Fields plugin
I created a new field in the editor where my colleagues can fill in a URL relevant to that specific page. This URL will be used in the button we load on every single job listing page. This button is created within the functions.php file:
function my_editor_content( $content ) {
$content = "<h3>Title</h3><p>text</P>
<a href='[acf field='afas_url']' target='_blank' rel='noopener'><button>Solliciteer Nu</button></a>";
The above is cut off at the 'afas_url' part. So, I think it has something to do with the quotations marks and probably not even hard to fix, but as I'm just starting to figure things out I couldn't find the answer myself yet.
I hope it's a clear enough explanation :)
(And maybe it's not best practice to use functions.php but it was already like this)

Replace in whole wordpress page html

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?

Replacing Wordpress comment_form

I'm trying to replace the comment_form with custom built solution. But I cannot seem to figure out how to do this and the code is lacking:
http://codex.wordpress.org/Function_Reference/comment_form
Ideally, anytime that the comment_form function is called, I want to replace it with an iframe to another site. Is there a way of doing this?
there is no way you can overwrite comment_form() function,
if you want to overwrite comment_form then its not possible in right way.
instead you can you use filter like comment_form_before and comment_form_after to achieve what you want.
i am putting this answers because i just see code of comment_form()
something like
<?php add_action('comment_form_before',function(){
echo '<div style="display:none;">';
});
add_action('comment_form_after',function(){
echo '</div><iframe><iframe>';
});
?>

Add paragraph tags to post content in wordpress?

I'm getting some pages with the get_pages function and echoing the page content like: $page->post_content, but contrary to the_content(), this way wordpress wont add p tags automatically, is there any way to add them here?
Thanks in advance
You should use <?php echo apply_filters('the_content', $page->post_content); ?>
Use the wpautop() function.
Jose Carlos' answer is actually the better approach. Out of the box, 'the_content' filter is loaded with the following actions:
capital_P_dangit
wptexturize
convert_smilies
convert_chars
wpautop
shortcode_unautop
prepend_attachment
So you can see that there's a lot more intelligence behind this filter. If you're positive that you don't need the other stuff (are you 100% sure you'll never have shortcode or smilies in your text?) then go ahead and use wpautop(), but you may regret it later on.
This might be what you're looking for, isn't it?
<?php
// Get WordPress pages
$wp_pages = get_pages();
foreach ($wp_pages as $wp_page)
{
echo '<p>';
echo $wp_page->post_content;
echo '</p>';
}

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!

Categories