Wordpress plugin add page frontend - php

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/

Related

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.

Wordpress Some custom posts don't render with archive

I have a Wordpress website that was about 5-6 custom post types. I have an archive.php and all the rest needed files. Most of them render good with my archive page but 2-3 render with my index.php.
What I tried:
I tried to make a page for each custom post, archive-{post-type} etc.. but that didn't work.
-
Does anyone have an idea about what might cause some of them to render good and only 2-3 to be problematic?
Go through the below following tips and snippet.
Remove if spaces on the top of your custom post archive file.
Go to Settings > Permalinks, change it to something else temporarily, press save, change it back to the original setting, press save again.
The below code snippet in your theme function.php
function get_custom_post_type_template($template) {
global $post;
if ($post->post_type == 'my_custom_post_type') {
$template = dirname( __FILE__ ) . '/archive-my_custom_post_type.php';
}
return $template;
}
//add_filter( "single_template", "get_custom_post_type_template" );//for single page
add_filter( "archive_template", "get_custom_post_type_template" ); //for archive

Setting Wordpress front page as Algolia search

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.

program custom wordpress plugin to output only to a specific page

I need to know how to take the output of a custom built Wordpress plugin and output it onto a specific page.
I know that I need to use the add_action() / add_filter() functions to call the function which outputs the plugins output when a wordpress hook function runs.
Currently I am using the 'the_content' hook.
This outputs my plugins output to all pages in my theme which call the the_content() function.
Which hook can I use to make the output only appear on a specific page.
Also
It would be useful to know how to create a page using my plugin.
Checkout is_page()
if (is_page(123)) {
// put your hooks here
}
put your code in a file inside plugin directory
then
use this
function page_directory()
{
if(is_page('123')){
$dir = plugin_dir_path( __FILE__ );
include($dir."custom/page.php");
die();
}
}
add_action( 'wp', 'page_directory' );

Enqueue Different Stylesheet in wordpress depending on user input via meta box

I am creating a wordpress plugin from which user can select different styles from the plugin options panel. I want to enqueue css file via wp_enqueue_style() according to the user input.
I am getting error when I want to get the metabox value inside the plugins php file.
Error Says:
Calling undefined variable $... on line no ..."
Is there any way I can get the value of the metabox inside php file and enqueue the appropiate css file according to the user input on dashboard.
I am using Custom-Metaboxes-and-Fields-for-WordPress plugin to retrieve user input from plugin option panel for this purpose
My code:
function easyloader_theme() {
global $post;
$theme = easyloader_get_option( 'easyloader_theme' );
wp_register_style('easyloader_style', plugins_url('themes/pace-theme-'. $theme .'.css',__FILE__),'','1.0.0.', false);
wp_enqueue_style('easyloader_style');
}
add_action( 'init', 'easyloader_theme');
I can easily get value of this user input in any when I paste this code in themes php file like (header.php, page.php, footer.php ) via
<?php global $post; $theme = easyloader_get_option( 'easyloader_theme' ); echo $theme; ?>
I just want to get the value inside my plugin php file not in the theme file.
The proper way to enqueue is with the following action:
add_action( 'wp_enqueue_scripts', 'easyloader_theme' );
function easyloader_theme() {
/* Enqueue Scripts AND Styles */
}
Looks like the plugin has some internal cache, but at the end you can try using WP function directly:
get_option( 'easyloader_theme' );

Categories