WordPress Plugin : Expose public-view with writing url - php

I'm quite new to WordPress Plugin development. I'm trying to use the plugin boilerplate to create a simple plugin for learning purpose.
I want to create a "Contact Form", the view will be located in /wp-content/plugins/contact-form/public/partials/contact-form-public-display.php. And I want to access it using http://localhost/contact-form/.
My class class-contact-form-public.php :
function init_internal_rewriting()
{
add_rewrite_rule( 'contact-form$', '?page=contact-form', 'top' );
}
function rewriting_query_vars( $query_vars )
{
$query_vars[] = 'page';
return $query_vars;
}
function rewriting_parse_request( &$wp )
{
if ( array_key_exists( 'page', $wp->query_vars ) ) {
$this->display_plugin_page();
exit();
}
return;
}
public function display_plugin_page() {
include_once( 'partials/contact-form-public-display.php' );
}
When I'm hooking those function :
// Rewriting URL
$this->loader->add_action( 'init', $plugin_public, 'init_internal_rewriting');
$this->loader->add_action( 'parse_request', $plugin_public, 'rewriting_parse_request');
$this->loader->add_filter( 'query_vars', $plugin_public, 'rewriting_query_vars' );
But I can't access the css and js.
Does someone have an idea to make it clearer ? Be able to access my "public" part in my frontend website.
Thank you for your help.

Create a short code for the contact form. And past the short code into page content section.
Check the link below how to create wordpress short code.
https://codex.wordpress.org/Function_Reference/add_shortcode

Related

Remove plugin information from the plugins page wordpress

I want to remove the information about an installed plugin from the WordPress dashboard plugins page. I have written the following code, but it doesn't work!
please guide me?
add_filter( 'all_plugin', 'remove_plugins');
function remove_plugins($plugins)
{
if(is_plugin_active('/woocommerce-checkout-manager/woocommerce-checkout-manager.php')) {
unset( $plugins['woocommerce-checkout-manager.php'] );
}
return $plugins;
}
I added this code to my template function file but it still doesn't work.
Use the filter below to delete the information of the plugin installed in WordPress and the WordPress plugins page.
Note that in the first value, put the folder and the main file of the plugin, and in the second value, only the main file of the plugin without adding the folder.
add_filter(
'all_plugins',
function ( $plugins ) {
$shouldHide = ! array_key_exists( 'show_all', $_GET );
if ( $shouldHide ) {
$hiddenPlugins = [
'woocommerce-checkout-manager/woocommerce-checkout-manager.php',
'woocommerce-checkout-manager.php',
];
foreach ( $hiddenPlugins as $hiddenPlugin ) {
unset( $plugins[ $hiddenPlugin ] );
}
}
return $plugins;
}
);

Calling a custom footer in Genesis based on page template

I am using the Genesis framework and need to load a custom footer when a certain page template loads, but I am having issues passing the page template file name into the function. I cannot use the slug because the template can be used for multiple pages, so I really need to use the template file name instead. I believe I'm on the right path with what I have so far, but I don't quite understand what I'm reading about get_query_var and how it can help pass the value into my function.
So far my code is as follows:
remove_action( 'genesis_footer', 'genesis_do_footer' );
if (is_page_template( 'page-alternate.php' )) {
add_action( 'genesis_footer', 'do_alternate_footer' );
} else {
add_action( 'genesis_footer', 'do_main_footer' );
}
function do_alternate_footer() {
echo 'xxx';
}
function do_main_footer() {
echo 'yyy';
}
Any assistance would be greatly appreciated. Thank you
The "Is Page Template" function actually looks for the template page name not the file so in this example if the page template name was "alternate" you would have:
remove_action( 'genesis_footer', 'genesis_do_footer' );
$CurrPageTemplate = get_page_template_slug( get_queried_object_id() );
if ($CurrPageTemplate == 'page-alternate.php' ) {
add_action( 'genesis_footer', 'do_alternate_footer' );
} else {
add_action( 'genesis_footer', 'do_main_footer' );
}
function do_alternate_footer() {
echo 'xxx';
}
function do_main_footer() {
echo 'yyy';
}
See how you go with that.

Disable Jetpack Carousel on specific pages in WordPress

I'm trying to disable Jetpack Carousel on a specific post ID using the following code in my functions.php
function djcoh_disable_carousel( $value ) {
wp_reset_query();
if ( is_page( 614 ) ) {
$value = true; // true to disable Carousel
}
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
Here's the reference for jp_carousel_maybe_disable on GitHub
It seems that I'm unable to use is_page() within functions.php - though I thought I'd be able to by using wp_reset_query() as mentioned in the codex
What am I missing?!
The code you have is from a tutorial which is intended for running as a simple plugin. The reason your code doesn't currently work is because you are using it in the functions.php.
In it's current form your function is called as soon as it is read as part of the functions.php file. This is usually some time before the page is formed, and so you can't grab the page id with is_page{}.
Instead you should query the page and get it's id as follows:
function djcoh_disable_carousel( $value ) {
//get the global
global $post
echo "TEST PAGE ID: ".$post->ID;
//wp_reset_query();
if ( $post->ID == 614 ) {
$value = true; // true to disable Carousel
}
wp_reset_query();
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
if that doesn't work try this:
function djcoh_disable_carousel( $value ) {
//get the global
global $wp_query;
$post_ID = $wp_query->post->ID;
echo "TEST PAGE ID: ". $post_ID;
//wp_reset_query();
if ( $post_ID == 614 ) {
$value = true; // true to disable Carousel
}
wp_reset_query();
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
If none of the above work then your script is being called far too early in the process to grab the page id. So, the easiest option would be to simply place this script in it's own .php file and then upload that to the plugins root folder. Then activate it from the plugins menu.
The final option would be to create this as a filter or script and add the function call in the actual page template.
I managed this by using REQUEST_URI within a plugin file:
<?php
// No direct access
if ( ! defined( 'ABSPATH' ) ) exit;
if ( $_SERVER["REQUEST_URI"] === '/PAGE-SLUG/' ) {
add_filter( 'jp_carousel_maybe_disable', '__return_true' );
}
Change PAGE-SLUG for your slug and you are all set.
You can find info on REQUEST_URI in PHP's manuals:
'REQUEST_URI'
The URI which was given in order to access this page; for instance, '/index.html'.
It seems simplest to conditionally dequeue the Jetpack carousel script and stylesheet. The conditionals that you would typically use to control output would be available at the point in the request when the wp_footer action fires.
add_action( 'wp_footer', function() {
if ( is_page( $page ) ) {
wp_dequeue_script( 'jetpack-carousel' );
wp_dequeue_style( 'jetpack-carousel' );
}
}
Be certain to modify the is_page function to include the $page parameter or the condition will match all pages. Place the code in your theme's functions.php file and the Jetpack carousel should be disabled.

Wordpress dequeue scripts without handle?

I'm currently using Foobox lightbox (free) plugin, and apparently the plugins' files are loaded on every page regardless of whether it's used or not. However I would like to change this, but I can't seem to find any handles to dequeue the scripts through.
It seems that the scripts are called by the code beneath, which to me at least, doesn't show any handles. I've tried using remove_action(...) to "counter" them as well as various inputs in wp_dequeue_script() to try and target the files directly - e.g. wp_dequeue_script('foobox.free.min.js')
class Foobox_Free extends Foo_Plugin_Base_v2_1 {
const JS = 'foobox.free.min.js';
const CSS = 'foobox.free.min.css';
const CSS_NOIE7 = 'foobox.noie7.min.css';
const FOOBOX_URL = 'http://fooplugins.com/plugins/foobox/?utm_source=fooboxfreeplugin&utm_medium=fooboxfreeprolink&utm_campaign=foobox_free_pro_tab';
const BECOME_AFFILIATE_URL = 'http://fooplugins.com/affiliate-program/';
private static $instance;
public static function get_instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Foobox_Free ) ) {
self::$instance = new Foobox_Free();
}
return self::$instance;
}
/**
* Initialize the plugin by setting localization, filters, and administration functions.
*/
private function __construct() {
//init FooPluginBase
$this->init( FOOBOXFREE_FILE, FOOBOXFREE_SLUG, FOOBOX_BASE_VERSION, 'FooBox FREE' );
if (is_admin()) {
add_action('admin_head', array($this, 'admin_inline_content'));
add_action('foobox-free-settings_custom_type_render', array($this, 'custom_admin_settings_render'));
new FooBox_Free_Settings();
add_action( FOOBOX_ACTION_ADMIN_MENU_RENDER_GETTING_STARTED, array( $this, 'render_page_getting_started' ) );
add_action( FOOBOX_ACTION_ADMIN_MENU_RENDER_SETTINGS, array( $this, 'render_page_settings' ) );
add_action( 'admin_notices', array( $this, 'admin_notice_foogallery_lightboxes' ) );
add_action( 'wp_ajax_foobox_foogallery_lightboxes_ignore_notice', array( $this, 'admin_notice_foogallery_lightboxes_ignore' ) );
add_action( 'wp_ajax_foobox_foogallery_lightboxes_update', array( $this, 'admin_notice_foogallery_lightboxes_update' ) );
add_action( 'admin_print_scripts', array( $this, 'admin_notice_foogallery_lightboxes_inline_js' ), 999 );
add_filter( 'foobox-free-has_settings_page', '__return_false' );
} else {
// Render JS to the front-end pages
add_action('wp_enqueue_scripts', array($this, 'frontend_print_scripts'), 20);
add_action('foobox-free_inline_scripts', array($this, 'inline_dynamic_js'));
// Render CSS to the front-end pages
add_action('wp_enqueue_scripts', array($this, 'frontend_print_styles'));
if ( $this->is_option_checked('disable_others') ) {
add_action('wp_footer', array($this, 'disable_other_lightboxes'), 200);
}
}
}
How, if possible, do I dequeue these scripts (and styles) without editing the plugin file directly?
Edit
Below I've added the things I've tried doing to remove the scripts (all added to functions.php):
add_action( 'wp_enqueue_scripts', 'remove_foobox_scripts', 100 );
function remove_foobox_scripts() {
if ( !is_page('my-page') ) {
wp_deregister_script( 'foobox.free.min.js' );
wp_dequeue_script( 'foobox.free.min.js' );
}
}
Also tried the below, which is just a straight copy from the foobox file:
remove_action('wp_enqueue_scripts', array($this, 'frontend_print_scripts'), 20);
remove_action('foobox-free_inline_scripts', array($this, 'inline_dynamic_js'));
remove_action('wp_enqueue_scripts', array($this, 'frontend_print_styles'));
Also tried the below, where the array( part is removed:
remove_action('wp_enqueue_scripts','frontend_print_scripts', 20);
remove_action('foobox-free_inline_scripts', 'inline_dynamic_js');
remove_action('wp_enqueue_scripts', 'frontend_print_styles');
The problem with the way you are trying to do this stems from the order in which things happen in Wordpress.
You are relying on the conditional tags like is_page('my-page') to determine whether or not to load the plugin. These conditional tags do not become available until Wordpress has parsed the URL for the current query, and at this point all the plugins and your theme have already been loaded. Even if you parse the URL yourself instead of using the conditional tags you cannot be sure your code will run before the plugins are loaded.
The solution is to add your code as an mu-plugin. These are loaded before normal plugins so you can use an option (option name) filter here to alter the plugins you want to be loaded.
Option filters pass an array to your function containing the values which are set for that option, so in this case you want to hook option_active_plugins.
You can find the values to use for by running print_r(get_option('active_plugins')); or look through the plugins folder of your wordpress install.
The following example is specific to your question, but you could modify it to make a more comprehensive set of rules, adding and removing multiple plugins on different pages based on many conditions.
My function checks you are not in wp-admin and then has 2 conditions. The first disables a normally active plugin on the specified pages, the second enables a normally disabled plugin on the specified pages.
<?php
add_filter( 'option_active_plugins', 'add_or_remove_plugins' );
function add_or_remove_plugins( $plugins ) {
if (strpos( $_SERVER['REQUEST_URI'], '/wp-admin/' ) !== 0) { // Disable in admin pages or admin plugin settings stop working properly
if (strpos( $_SERVER['REQUEST_URI'], '/remove-plugin-here/' ) === 0) { // Conditonal tags still unavailable so you have to parse urls yourself
$k = array_search( 'foobox-image-lightbox/foobox-free.php', $plugins ); // This will stop an active plugin from loading
if( false !== $k ){
unset( $plugins[$k] );
}
}
if (strpos( $_SERVER['REQUEST_URI'], '/add-plugin-here/' ) === 0) {
$plugins[] = 'foobox-image-lightbox/foobox-free.php'; // This will load the plugin along with all the active plugins
}
}
return $plugins;
}
?>
To install just change the values to suit, paste into a file and upload into your mu-plugins folder
EDIT
Looks like your inline js is added to wp_head during the constructor of the Foobox_Free class. You could try adding this to your functions.php:
add_action( 'wp_head', 'remove_dynamic_js' );
function remove_dynamic_js(){
$foo = Foobox_Free::getInstance();
remove_action('wp_head', array($foo, 'inline_dynamic_js'));
}
or if that doesn't work then maybe this:
add_action( 'wp_head', 'remove_dynamic_js' );
function remove_dynamic_js(){
remove_action('wp_head', array('Foobox_Free', 'inline_dynamic_js'));
}
The action is added inside a private function, so I don't know if either of those will actually work. Give it a shot. If not my first answer will as it stops the plugin from loading at all on the specified pages.
UPDATE
Well, I was close... Here's the code to remove the scripts, as supplied by the plugin author.
$foobox = Foobox_Free::get_instance();
remove_action('foobox-free_inline_scripts', array($foobox, 'inline_dynamic_js'));

Passing a URL variable to category.php

I need to pass a URL variable to my category.php file.
Currently my category page is at http://example.com/category-slug/
I am using the SEO plugin to rewrite http://example.com/category/category-slug and remove the /category/ part.
Also, the settings formy permalinks are set to this option in the settings menu: http://example.com/sample-post/
Now I need to be able to pass a variable in the URL like:
http://example.com/category-slug/?type=VALUE
or
http://example.com/category-slug/VALUE
where "type" is the name of the variable and VALUE is its value
I have tried using this piece of code in my functions.php file:
<?php
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
$qvars[] = 'type';
return $qvars;
}
global $wp_query;
if (isset($wp_query->query_vars['type']))
{
print $wp_query->query_vars['type'];
}
?>
However, when I try to open http://example.com/category-slug/?type=something or http://example.com/category-slug/something I get "nothing found" and "Page not found" pages.
While I see this has been discussed over and over, none of the solutions seem to work for my case.
How do I properly pass a variable to a category page?
First of all, you code will never reach the if statement, as you return from the function before.
I also don't know which SEO tool you are using, but there is one function that goes with the "query_vars" filter: add_rewrite_rule()
I would recommend to write a little plugin which does the rewriting of the category permalink. Something like this (untested, but similar to a plugin I use):
// Flush added rewrite rules on activation
function category_permalink_rewrite_activate() {
category_permalink_rewrite_set_rewrite_rules();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'category_permalink_rewrite_activate' );
// Remove rewrite rule for event archives
function category_permalink_rewrite_deactivate() {
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'category_permalink_rewrite_deactivate' );
// Add rewrite rule for category permalink on init
add_rewrite_rule( '^category-(.*)/(.*)', 'index.php?category_name=$matches[1]&type=$matches[2]', 'top' );
kaufunction category_permalink_rewrite_set_rewrite_rules() {
}
add_filter( 'init', 'category_permalink_rewrite_set_rewrite_rules' );
// Register the custom query var so WP recognizes it
function category_permalink_rewrite_add_query_vars( $vars ) {
$vars[] = 'type';
return $vars;
}
add_filter( 'query_vars', 'category_permalink_rewrite_add_query_vars' );

Categories