Setting Wordpress front page as Algolia search - php

I've recently setup a new Wordpress install to act as a survey database. The purpose of the site is to collect survey data and allow the admin's to filter and search submitted survey data.
I've installed and configured the Algolia search WP plugin. Everything is working properly. If I navigate to 'mydomain.com/?s=' I see the search form and it's returning results.
My question is how can I set the Algolia search page as my Wordpress index/front page? Or, how can I import this form to a page that I can designate as my WP static front page?
Further info: I have a child theme installed and can create custom page templates/template-parts

The reason is this code here...
if ( is_search() && $settings->should_override_search_with_instantsearch() ) {
return $this->load_instantsearch_template();
}
from this file
https://github.com/algolia/algoliasearch-wordpress/blob/1a51d3e2e8be12bfcbccd1ef2a722911a99376e7/includes/class-algolia-template-loader.php
Essentially it isn't being loaded at present where you want.
Putting this code in your functions.php will fix it.
add_action( 'wp_enqueue_scripts', function () {
// Enqueue the instantsearch.js default styles.
wp_enqueue_style( 'algolia-instantsearch' );
// Ensure jQuery is loaded.
wp_enqueue_script( 'jquery' );
// Enqueue the instantsearch.js library.
wp_enqueue_script( 'algolia-instantsearch' );
// WordPress utility useful for using underscore templating.
wp_enqueue_script( 'wp-util' );
// Allow users to easily enqueue custom styles and scripts.
do_action( 'algolia_instantsearch_scripts' );
} );
Then just add the instantsearch.php code or include the file to the index page/page you want to load it on.
(I just replaced the index.php code with the code from instantsearch.php and it worked just fine)
Hope that helps.

Related

How to remove the woocommerce.css file from the theme?

I am using WordPress and I installed the Woocommerce plugin.
I added the template folder in my theme and started to customize my theme.
Now, I have to remove the Woocommerce.css from my theme and I found code on the official website here
I added the same code in the function.php
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
or
add_filter( 'woocommerce_enqueue_styles', '__return_false' );
but both answers are not working. I have installed the 5.7.1 Woocommerce.
How can I solve this issue?
function.php
function customtheme_scripts() {
wp_enqueue_style( 'customtheme-style', get_stylesheet_uri(), array(), time() );
wp_style_add_data( 'customtheme-style', 'rtl', 'replace' );
wp_enqueue_script( 'customtheme-navigation', get_template_directory_uri() . '/js/navigation.js', array(), _S_VERSION, true );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
wp_dequeue_style( 'customtheme-woocommerce-style' );
}
add_action( 'wp_enqueue_scripts', 'customtheme_scripts' );
view source
The domain name is just an example.
This answer has been fully tested on woocommerce 5.x+ and works fine on the default woo style sheets! If you're using a custom theme and/or custom style sheet then you may encounter some discrepancies.
What you see on the documentation page, no longer works on woo 4+, according to their github page.
So you need to dequeue its styles!
wp_dequeue_styleDocs
So if you only want to remove woocommerce.css file, then you could do this:
add_action('wp_enqueue_scripts', 'removing_woo_styles');
function removing_woo_styles()
{
wp_dequeue_style('woocommerce-general'); // This is "woocommerce.css" file
}
However, if you want to remove all of the style sheets loaded by woo, then you could use this:
add_action('wp_enqueue_scripts', 'removing_woo_styles');
function removing_woo_styles()
{
wp_dequeue_style('wc-block-vendors-style');
wp_dequeue_style('wc-block-style');
wp_dequeue_style('woocommerce-general');
wp_dequeue_style('woocommerce-layout');
wp_dequeue_style('woocommerce-smallscreen');
}
If you still can see the styles, then try to clean your cache.
UPDATE related to the question "custom style sheet"
At the time that I was writing the answer you had not provided any screenshot of your style sheet, nor did you say anything about using a custom style sheet. That's why you were not able to get it to work.
Please do NOT copy/paste if you're using a custom style sheet, like the custom css file used in the question. wp_dequeue_style function takes your style sheet handle as the argument. So please read the documentation first. You're using a custom handle name (i.e "customtheme-woocommerce-style"), therefore, you need to use that handle name.
add_action('wp_enqueue_scripts', 'removing_woo_styles');
function removing_woo_styles()
{
wp_dequeue_style('customtheme-woocommerce-style'); // This is your "custom style sheet" file.
}
Also note that commenting out the enqueue section in the main file (i.e inc/woocommerce.php) may work temporarily but on the next woo update, it'll come back again. So, it's recommended to avoid updating your template files as much as possible unless you really have to which is not the case here!

How to PREVENT a plugin to run on a SPECIFIC PAGE?

I am looking for a solution that will NOT activate a certain plugin on a SINGLE PAGE.
I have tried to play around with the attached code (offered by #Kamil Grzegorczyk on Wordpress Disable Plugin on Specific Pages/Posts) but with no success.
add_filter( 'option_active_plugins', 'lg_disable_cart66_plugin' );
function lg_disable_cart66_plugin($plugins){
if(strpos($_SERVER['REQUEST_URI'], '/store/') === FALSE AND strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === FALSE) {
$key = array_search( 'cart66/cart66.php' , $plugins );
if ( false !== $key ) unset( $plugins[$key] );
}
return $plugins;
}
I have also tried to us plugin organizer but it is not helping to NOT activate a certain plugin on a SINGLE PAGE.
BTW, if anyone got a code that can only activate certain plugins such as Contact form no 7 by tracing their shortcode on a post/page that will be brilliant!
Something like if x page/post has a shortcode that contains 'contact form no 7' then activate Contact form no 7 plugin, else, do not activate.
Anyway, at this stage, an easy to go solution will do :)
Thanks.
"BTW, if anyone got a code that can only activate certain plugins such as Contact form no 7 by tracing their shortcode on a post/page that will be brilliant! Something like if x page/post has a shortcode that contains 'contact form no 7' then activate Contact form no 7 plugin, else, do not activate." - This is not possible. Reading the source code wp-blog-header.php should make this clear.
$wp_did_header = true;
// Load the WordPress library.
require_once( dirname( __FILE__ ) . '/wp-load.php' );
// Set up the WordPress query.
wp();
// Load the theme template.
require_once( ABSPATH . WPINC . '/template-loader.php' );
Plugins are loaded by wp-settings.php which is loaded by wp-load.php. The post content of a request is retrieved from the database by WP::query_posts() which is called by the function wp(). So, plugins are already loaded before the post content is even retrieved from the database.
Where did you put the code:
add_filter( 'option_active_plugins', 'lg_disable_cart66_plugin' );
This cannot be put in a regular plugin since it must execute BEFORE plugins are loaded. (You cannot tell a plugin not to load in code that is already loaded.) It also cannot be put in functions.php since functions.php is loaded AFTER plugins are loaded. Did you read the comment about mu-plugins? "must use" plugins are loaded before regular plugins. All of this can be found in wp-settings.php - I highly recommend reading it - it is an easy read and you will understand how WordPress initializes itself.

Including a Jquery file for form validation in a child theme

I've created a child theme and am running my site on that so that I can customise a form that I'm including on certain pages using the plugin 'contact-form-7". In my child folder I've placed a style.css, functions.php and js/custom_script.js. the style sheet is being imported fine but I can't seem to get my validation jquery file working on that form. This is the code I've been using:
add_action( 'wp_enqueue_scripts', 'add_my_script' );
function add_my_script() {
wp_register_script( 'custom_script', get_stylesheet_directory_uri() . '/js/custom_script.js' ,array( 'jquery'));
wp_enqueue_script( 'custom_script' );
}
console isn't logging any errors but I can't seem to find it in the web tools side bar UNLESS I remove "js/" when the custom_script is called, in which case a GET error pops up.
I'm guessing I'm incorrectly importing the file and honestly I can't quite get my head around these hooks and importing files just yet- only been using wordpress a short time.
Any help much appreciated.
I believe that you haven't included the JQuery file? You have to download it first https://jquery.com/download/, and then register it as you did with "custom_script", you just have to put it before the "custom_script" in functions.php, inside "custom_script.js" you could also do:
$(document).ready(function(){
});
Go to your console and if you see:
Uncaught ReferenceError: $ is not defined
It's because you haven't included the JQuery file as needed.
Hope this helps!
Leo.

Wordpress plugin add page frontend

I'm developing a plugin for wordpress. This plugin has to have an admin section for plugin set up but also has to have a custom front end page with forms.
I'm new into the wordpress plugin development world but I have not found specific information for this task.
Is there a way to add pages to front end from a plugin or is necessary to manually edit the current template and add the page?
Here is a way to add custom content on a front-end page when you create your plugin: http://blog.frontendfactory.com/how-to-create-front-end-page-from-your-wordpress-plugin/
function elegance_referal_init()
{
if(is_page('share')){
$dir = plugin_dir_path( __FILE__ );
include($dir."frontend-form.php");
die();
}
}
add_action( 'wp', 'elegance_referal_init' );
To add a page to Wordpress that runs custom code without adding the page as a post or page in the database, try this in your plugin file.
add_action ('pre_get_posts', function ($query) {
global $wp;
if ($wp->request == 'pageurl'){
$dir = plugin_dir_path( __FILE__ );
include $dir . "your_file.php";
die();
}
});
I think you looking for this for the admin settings page https://codex.wordpress.org/Function_Reference/add_options_page
Fot the front end, i would recommend to create a page with a template https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/

Why wp_register_script doesn't work for register_activation_hook

I want to register a script using when my plugin is activated. So I placed:
register_activation_hook(__FILE__, 'register_script');
function register_script(){
wp_register_script('addjs','http://example.com/exmple.js');
}
Next I created a meta box and enqueue the script in the add_meta_boxes hook:
wp_enqueue_script('addjs');
Why that doesn't work? My JS file is just a simple alert function. However, why when I register my script using init hook, or other hook, my code works?
Registering the script doesn't actually enqueues the script. And that's what needs to be optimized.
register_activation_hook runs only once, registering your script there won't keep it registered in subsequent WordPress loads. Neither the action hook init is the place to do this. It's either wp_enqueue_scripts (frontend) or admin_print_scripts (backend), despite their names they're used to enqueue styles and scripts.
Loading your scripts only in specific places is a great practice. Once my admin area broke because a theme was loading its scripts/styles everywhere, not only on the frontend. Crappy code at its best.
The main difference is that if you register a script, you'll only need to call wp_enqueue_script($handle) in other places of your code. But if it's not the case, you can drop the register part and enqueue directly.
A small trick is to add your wp_enqueue_script and wp_enqueue_style inside your add_meta_box() callback, and it will only load together with the Meta Box. Works the same for Shortcodes.
Another option,
add_action( 'admin_menu', function()
{
$page = add_submenu_page( $args );
add_action( "admin_print_scripts-$page", 'your_callback' );
});
Or,
add_action( 'wp_enqueue_scripts', function() {
if( is_single() )
wp_enqueue_style( $args );
});
References:
- conditional-wp-enqueue-script-on-a-page
- when-to-use-add-actioninit-vs-add-actionwp-enqueue-scripts
- register-and-enqueue-conditional-browser-specific-javascript-files
- how-to-load-scripts-and-css-for-admins-only-when-editing-or-adding-posts

Categories