Remove YouTube embedding with wp_oembed_remove_provider() - wordpress

I am trying to remove YouTube embedding from some of my WordPress output, so it only shows the link.
I have tried the following code without any luck.
wp_oembed_remove_provider('#http://(www\.)?youtube\.com/watch.*#i');
wp_oembed_remove_provider('#https://(www\.)?youtube\.com/watch.*#i');
wp_oembed_remove_provider('#http://youtu\.be/.*#i');
wp_oembed_remove_provider('#https://youtu\.be/.*#i');
echo apply_filters("the_content", $result->text);
What am I doing wrong? And how am I doing this correctly?

Chances are you're at a point where it's too late to remove the providers. Try hooking into plugins_loaded or init:
add_action( 'init', 'so26743803_wp_oembed_remove_provider' );
function so26743803_wp_oembed_remove_provider(){
wp_oembed_remove_provider('#https://youtu\.be/.*#i');
// etc.
}
Edit (untested): unsetting providers with the oembed_providers filter (see this Q):
add_filter( 'oembed_providers', 'so26743803_oembed_providers' );
function so26743803_oembed_providers( $providers )
{
unset( $providers['#http://youtu\.be/.*#i'] );
// etc.
return $providers;
}

Related

how to remove "added-to-cart notice" from every page except product archive pages?

Note: In looking for an answer to my question I came across this post but it is NOT duplicate: Remove add to cart notice and change "add to cart" button in Woocommerce the answer there gives the option to remove the notice from the entire site. I want to remove it only from the cart page and I don't want to do it with CSS.
I use external links to my site to send people directly to the shopping cart with the item already added to the cart. When doing so, the "added-to-cart notification" shows up on the cart page which I do not want.
I found this code which removes the added-to-cart notification: add_filter( 'wc_add_to_cart_message_html', '__return_false' ); but it removes the notification from all pages of my site which is not what I want.
To be more specific, I want the added-to-cart notification to show on every product archive page and nowhere else.
I tried to add a filter but it doesn't work the way I would expect it to, I tried the following two ways (and tested it with various pages to see if I could make anything work but it seems my general syntax is off because I Can't get it to do anything...
function hide_cart_notes() {
if ( ! is_archive() ) {
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
}
}
add_action( 'woocommerce', 'hide_cart_notes' );
function hide_cart_notes() {
if ( is_archive() ) {
return;
}
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
}
add_action( 'woocommerce', 'hide_cart_notes' );
when woocommerce hook starts? where it's docs? does it run at all?
these question should be answered before.
i know that WordPress parses query at parse_query hook, so i would try this
add_action('parse_query', function() {
if (!is_archive()) {
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
}
});
because is_shop(), is_archive(), is_* need query to be parsed first.

Remove Gutenberg CSS

I have Gutenberg plugin installed in WordPress v4.9.8 and am trying to remove the CSS that comes with it so I can supply my own.
This is the sheet that gets included:
<link rel='stylesheet' id='wp-block-library-css' href='/wp-content/plugins/gutenberg/build/block-library/style.css?ver=1535795173' type='text/css' media='all' />
I have tried the following:
add_action( 'wp_print_styles', 'wps_deregister_styles', 100 );
function wps_deregister_styles() {
wp_dequeue_style( 'wp-block-library-css' );
wp_deregister_style( 'wp-block-library-css' );
}
As well as variations of this, but the file persists. How can I remove it?
I'm adding this as a more complete answer than my comment:
You need to remove the -css when trying to dequeue the script. That's added to the HTML markup and not the actual tag for the css file.
If you search the code (the location of the enqueue may change as Gutenberg gets rolled into core), you can find:
wp_enqueue_style( 'wp-block-library' );
As you can see, there is no -css. This solution may work for other plugins that people have trouble dequeuing styles.
Edit:
Since this still gets some traction, here is the code to handle it:
add_action( 'wp_print_styles', 'wps_deregister_styles', 100 );
function wps_deregister_styles() {
wp_dequeue_style( 'wp-block-library' );
}
I am use this code to to remove default style.
//Disable gutenberg style in Front
function wps_deregister_styles() {
wp_dequeue_style( 'wp-block-library' );
}
add_action( 'wp_print_styles', 'wps_deregister_styles', 100 );
I use Wordpress 5.1. Tried the most upvoted answers and they didn't work for me, 'wp_enqueue_scripts' instead of 'wp_print_styles' does the trick.
Here is my whole WordPress 5.1 solution to get rid of Gutenberg without bloat stylesheets loading:
// Disable Gutenberg editor.
add_filter('use_block_editor_for_post_type', '__return_false', 10);
// Don't load Gutenberg-related stylesheets.
add_action( 'wp_enqueue_scripts', 'remove_block_css', 100 );
function remove_block_css() {
wp_dequeue_style( 'wp-block-library' ); // Wordpress core
wp_dequeue_style( 'wp-block-library-theme' ); // Wordpress core
wp_dequeue_style( 'wc-block-style' ); // WooCommerce
wp_dequeue_style( 'storefront-gutenberg-blocks' ); // Storefront theme
}
Edit:
It works with WordPress 5.2 as well and because it handles the stylesheets added by WooCommerce and Storefront theme, I made this one of the settings in my new plugin:
https://wordpress.org/plugins/extra-settings-for-woocommerce/
Paste the following code on your functions.php file
function custom_theme_assets() {
wp_dequeue_style( 'wp-block-library' );
}
add_action( 'wp_enqueue_scripts', 'custom_theme_assets', 100 );
Please like if this has helped you.
As the wp_dequeue_style-approach did not work to disable wp-editor-font (wp-editor-font-css) I used the following code:
function my_remove_gutenberg_styles($translation, $text, $context, $domain)
{
if($context != 'Google Font Name and Variants' || $text != 'Noto Serif:400,400i,700,700i') {
return $translation;
}
return 'off';
}
add_filter( 'gettext_with_context', 'my_remove_gutenberg_styles',10, 4);
Also see https://github.com/dimadin/disable-google-fonts/blob/master/disable-google-fonts.php
In 2021 I tried variations of most of the approaches above and they all failed. Looking at the WordPress code, I believe the reason why is that Gutenberg styles are not enqueued anymore. The only way I have found to remove it is to remove it just before it is printed.
// This is what works for me.
add_action( 'wp_print_styles', 'wps_deregister_styles', 9999 );
function wps_deregister_styles() {
global $wp_styles;
$wp_styles->remove('wp-block-library');
}
This doesn't really answer the question, but I suspect others like me have ended up here trying to resolve something this question alludes to but doesn't address: how do you remove the inline (WP 5.5+) storefront-gutenberg-blocks-inline-css so you can use the editor even though it's using white on white or black on black in the storefront theme?
The following will do just that. Put it in your functions.php file or a plugin.
function clean_storefront_gutenberg_block_editor_customizer_css( $string ) {
// return empty so the editor doesn't suck
return '';
}
add_filter( 'storefront_gutenberg_block_editor_customizer_css', 'clean_storefront_gutenberg_block_editor_customizer_css' );
This just defuses the generated CSS so there's nothing appended to the back-end editor. The front-end remains unchanged.
Remove Gutenberg Block Library CSS from loading on the frontend
function smartwp_remove_wp_block_library_css()
{
wp_dequeue_style('wp-block-library');
wp_dequeue_style('wp-block-library-theme');
wp_dequeue_style('wc-block-style'); // Remove WooCommerce block CSS
}
add_action('wp_enqueue_scripts', 'smartwp_remove_wp_block_library_css', 999);
None of the above answers removed all the styles in the block editor, the following worked for me:
add_action( 'admin_print_styles', function() {
global $wp_styles;
$stylesToRemove = ["wp-reset-editor-styles", "wp-format-library", "wp-block-library", "wp-block-library-theme", "wp-block-directory", "wp-edit-blocks"];
foreach ($stylesToRemove as $styleToRemove) {
foreach ($wp_styles->registered as $style) {
$dep = array_search($styleToRemove, $style->deps);
if ($dep !== false) {
unset($style->deps[$dep]);
}
}
wp_deregister_style($styleToRemove);
}
}, 1 );
add_action('wp_enqueue_scripts', 'wps_deregister_styles', 200);
Works for me.
As the latest Gutenberg update has been released recently, a lot of people are wondering how to remove wp-block-library from WordPress. As the guide shows, below you need you to reference the 100 in your add_action.
Firslty, you must create a function that holds your wp_dequeue_style for wp-block-library and then call it as such:
add_action( 'wp_enqueue_scripts', 'custom_function', 100 );

Woocommere checkout page error?

I'm trying to find the origin of this error but i can't find it.
This error randomly shows up on the checkout page, any idea what could be causing this?
If you not neccesary to use postal code you can disable them by adding this function to yout functions.php
function custom_override_default_address_fields( $address_fields )
{
unset( $address_fields['postcode'] );
return $address_fields;
}

How to get the whole html content when visiting a page of splited wordpress post

I am doing string conversion for every post/page in Wordpress. I add below function to template_redirect hook to get the whole html content and do the conversion.
function my_string_conversing() {
obstart( 'ob_callback' );
return;
}
function ob_callback( $buffer ) {
// doing conversion here...
return $buffer
}
add_action( 'template_redirect', 'my_string_conversion' );
These code works well for posts and pages, but if I split one post into multiple pages, then this will not work. It even don't run my_string_conversion function when click each page of a split post. How can I make it work? Thanks.
I found the solution, use add_action( 'template_redirect', 'my_string_conversion', -100 ); instead.

Wordpress Plugin: Show html only on standard page and not in admin area

I'm writing a plugin and I need to display a piece of text in the WP page, but not in the admin area. How can I do so?
I tried this in the construct:
add_action( 'init', array( $this, 'initPage' ) )
and then:
public function initPage() {
echo 'hello';
}
but the text is displayed also in the admin area. Is there a way to do this? It would be the opposite of the action admin_init I assume.
Proper way to handle it: is_admin()
http://codex.wordpress.org/Function_Reference/is_admin
if(is_admin()) { // do nothing } else {
// function you want to execute.
}
I solved this by adding it to a shortcode action. Like this:
add_shortcode( 'myPlugin', array( $this, 'shortcode' ) );
and:
public function shortcode( $atts ) {
return 'hello';
}
With the above code, 'hello' will only display on the front-end. Not sure if that's the cleaner way to do it, but does the job.
There is no "front-end-only" version of init, however you probably don't want to be doing any output at the init action anyway.
What exactly are you trying to do? Usually, you use an action hook for specific types of things, and causing output very early at something like "init" is rare and weird.

Resources