WordPress Function on first Theme Activation - wordpress

is there a way to execute a function only on the first activation of a specific theme?
I found this code but the problem is that the function will execute after every theme switch
<?php add_action("after_switch_theme", "mytheme_do_something"); ?>

Yes you can by writing and checking an option.
add_action("after_switch_theme", "mytheme_do_something");
function mytheme_do_something() {
if( ! get_option( 'mytheme_setting' ) ) {
add_option( 'mytheme_setting', '1');
// some things you want to do
}
}

Related

getting desktop-columns-3 tablet-columns-2 mobile-columns-1 on my website

Recently I was performing changes with my wordpress website. I am getting desktop-columns-3 tablet-columns-2 mobile-columns-1 on my every product page.
I am clueless since when I search for this error it is only telling me about the sizing not any reason why I am getting this written everywhere.
Getting issue with all product pages
I had this problem too and this is how I fixed it.
Go to wp-content/plugins/woocommerce/includes/wp-template-functions.php
Line 708 has the line:
$loop_start = apply_filters( 'woocommerce_product_loop_start', ob_get_clean() );
That line is already defined in several places, so i've commented it out and it's resolved the problem. I don't know if it's broken something somewhere else but everything seems to be working fine for now.. If it does break something my next idea is make it "echo false" or something.
Either way, this seems to work for now. Good luck with your site!
At your theme find a file function.php
add new function like this :
<?php
// add any new or customised functions here
function woocommerce_product_loop_start() {
ob_start();
wc_set_loop_prop( 'loop', 0 );
wc_get_template( 'loop/loop-start.php' );
$loop_start = apply_filters( 'woocommerce_product_loop_start', 'ob_get_clean()' );
return $loop_start;
}
it will replace the original function from plugin. Hope it will be save your problem
Look for Function woocommerce_product_loop_start in the wp-content / plugins / woocommerce / includes / wp-template-functions.php file and replace it with the following code:
if ( ! function_exists( 'woocommerce_product_loop_start' ) ) {
/**
* Output the start of a product loop. By default this is a UL
*
* #access public
* #param bool $echo
* #return string
*/
function woocommerce_product_loop_start( $echo = true ) {
ob_start();
wc_get_template( 'loop/loop-start.php' );
if ( $echo )
echo ob_get_clean();
else
return ob_get_clean();
}
}

Relocate wordpress plugin output thats hooked into the content

I am trying to relocate the position of the output generated by a review plugin for wordpress. The plugin hooks into the end of "the_content". I have found the filter which works to remove the output:
remove_filter('the_content', array( EDD_Reviews::get_instance(), 'load_frontend'));
Now I am going crazy trying to relocate the output to a new location. I have created a custom hook in my theme file:
function reviews() {
do_action('reviews');
}
The above hook outputs just fine to custom location in the theme files when passed a simple function. However the tricky part is getting the reviews and review form to display. I tried adding it back with this:
add_action('reviews', array( EDD_Reviews::get_instance(), 'load_frontend'));
This did not work. The full function I am trying to call is below. Any body have any ideas how I may call this via my new hook? I am rather stuck.
public function load_frontend( $content ) {
global $post;
if ( $post && $post->post_type == 'download' && is_singular( 'download' ) && is_main_query() && ! post_password_required() ) {
ob_start();
edd_get_template_part( 'reviews' );
if ( get_option( 'thread_comments' ) ) {
edd_get_template_part( 'reviews-reply' );
}
$content .= ob_get_contents();
ob_end_clean();
}
return $content;
}
Many thanks in advance.
Ok mostly figured this out after studying classes. If anyone else is looking to do this then remove the hook as below:
remove_filter('the_content', array( EDD_Reviews::get_instance(), 'load_frontend'));
The following line will then render the reviews and form anywhere you like:
<?php echo EDD_Reviews()->load_frontend( '' ); ?>

Remove JS file from Woocommerce Product edit pages

I am getting this error "Uncaught Error: Option 'ajax' is not allowed for Select2 when attached to a element." while updating Product Variation.
Actually there are 2 select2.js files, one from Woocommerce and other from 'WR PageBuilder' plugin. While I am renaming 'WR PageBuilder' select2.js file then its working fine. But that file is required for Editor.
I want to remove that js file only from Product pages.
I did 'wp_deregister_script()' and 'wp_dequeue_script()' but nothing happened.
Here is my code:
add_action('admin_init', 'functon_to_filter_script');
function functon_to_filter_script() {
global $typenow;
// when editing pages, $typenow isn't set until later!
if (empty($typenow)) {
// try to pick it up from the query string
if (!empty($_GET['post'])) {
$post = get_post($_GET['post']);
$typenow = $post->post_type;
}
}
if( 'product' == $typenow ){
add_action( 'admin_enqueue_scripts', 'deregister_my_script', 100 );
}
}
function deregister_my_script() {
wp_dequeue_script('wr-pagebuilder');
wp_deregister_script('wr-pagebuilder');
}
can anyone give me a solution?
This won't work because you are using the actions wrong.
Look here for the correct usage of action hooks:
Hooks in Wordpress
You are putting the admin_enqueue_scripts action hook inside of the admin_init action hook.
Try taking admin_enqueue_scripts outside of the admin_init hook like this:
global $typenow;
add_action( 'admin_enqueue_scripts', 'deregister_my_script', 100 );
function deregister_my_script() {
if (!empty($_GET['post'])) {
$post = get_post($_GET['post']);
$typenow = $post->post_type;
}
if( 'product' == $typenow ){
wp_dequeue_script('wr-pagebuilder');
wp_deregister_script('wr-pagebuilder');
}
}

PHP include for just homepage in wordpress (genesis framework)

I'm using the Genesis framework and trying to add some bits under the content.
I want them to appear on just the homepage, so I'm currently using this code, however it's including on all pages.
I have a feeling the if statement is wrong...
add_action( 'genesis_after_content', 'sp_homepage_content' );
function sp_homepage_content() {
if ( is_page('2') )
include ('includes/homepage-content.php');
}
EDIT:
I think i fixed it with the following:
add_action( 'genesis_after_content', 'sp_homepage_content' );
function sp_homepage_content() {
?>
<?php if ( is_page('2') ) { include ('includes/homepage-content.php');} else ?>
<?php
}
Although I'm not sure if that's "good" code... It works
Change your condition to if(is_home()).
add_action( 'genesis_after_content', 'sp_homepage_content' );
function sp_homepage_content() {
if ( is_home() )
include ('includes/homepage-content.php');
}

Do I have to add_filter() before apply_filters() in Wordpress?

I'm trying to understand Wordpress plugin like:
apply_filters( 'gettext', $translations->translate( $text ), $text, $domain );
I'm looking for all codes in Wordpress, I can't find:
add_filter( 'gettext', ....);
Why there is no add_filter for this plugin? Or I missed something? Same thing like:
do_action('wp_loaded');
I can't find:
add_action('wp_loaded', ....);
apply_filters is like, 'if there are any filters with this name, run the attached callbacks with these parameters'. So if there is no add_filter for that name, it means that there is no filter that's going to be run with the apply_filters call at the moment.
The same goes with do_action and add_action.
I am a beginner in PHP - WordPress stack as well, but this is from my understanding.
The plugins call apply_filters without having any add_filter in their codes is to allow the website users to add custom logic to their plugins. We - the users, can add our own function and use add_filter to register our functions.
For example, this piece of code is from the plugin. Normally, it shows all products but it provides us a way to not show a specific product.
// Plugin's
if (apply_filters( 'plugin_show_products', true, $product->get_id() ) ) {
$this->show_products();
}
So, if we - the users, want to customize a bit. We can add our own function as following (maybe in functions.php)
// Our custom changes
function my_own_changes($boolean, $product_id) {
if ( $product_id === 5 ) return false;
return true;
}
add_filter( 'plugin_show_products', 'my_own_changes', 10, 2 );
This translates to: The plugin will behave normally but for my own site, it will not show the product with ID of 5!
I have come across this type of code in a plugin or theme where the apply_filter is used without necessarily having an existing filter or add_filter
In this case, where the apply_filters is used without a filter you will have to call the function again where you want to run it. For example, in the header of a theme.
The following is an example of apply filters used in a function that is again called in the header.php
if ( ! function_exists( 'header_apply_filter_test' ) ) {
function header_apply_filter_test() {
$filter_this_content = "Example of content to filter";
ob_start();
echo $filter_this_content;
$output = ob_get_clean();
echo apply_filters( 'header_apply_filter_test', $output );//used here
}
}
Now in the header.php file, you would have to call this function since it is not hooked anywhere. So, in this case, to display the output in the header you would call the function like this :
<?php header_apply_filter_test(); ?>
You could as well write this code with a hook and it would do the same thing i.e display the output in the header.
add_filter('wp_head', 'header_apply_filter_test');
if ( ! function_exists( 'header_apply_filter_test' ) ) {
function header_apply_filter_test() {
$filter_this_content = "Example of content to filter";
ob_start();
echo $filter_this_content;
$output = ob_get_clean();
echo $output;
}
}
For this second option, you would still have the capability of using apply_filters anywhere else to call the callback function header_apply_filter_test() since the filter now exists.
So the bottom line in my view is a use case since either approach works!

Resources