Wordpress Plugin Dynamic Error Page - php

What action or filter can I use in a Wordpress plugin to dynamically replace the contents (i.e. not the header or footer) of a 404 error page?
Basically I'm looking for a 404 error page equivalent for the_content filter, which will filter the contents of an existing page.
Thank you for your time.
Note: I know I can manually modify the 404 error page for the current theme, but that is not the effect I am trying to achieve.

From this WordPress Answer: How to control output of custom post type without modifying theme?
Plugin file:
<?php
/*
Plugin Name: Plugin 404 Page
Plugin URI: http://stackoverflow.com/questions/14539884
Description: Use the plugin's template file to render a custom 404.php
Author: brasofilo
Author URI: https://wordpress.stackexchange.com/users/12615/brasofilo
Version: 2013.26.01
License: GPLv2
*/
class Universal_Template
{
public function __construct()
{
$this->url = plugins_url( '', __FILE__ );
$this->path = plugin_dir_path( __FILE__ );
add_action( 'init', array( $this, 'init' ) );
}
public function init()
{
add_filter( 'template_include', array( $this, 'template_404' ) );
}
public function template_404( $template )
{
if ( is_404() )
$template = $this->path . '/404.php';
return $template;
}
}
$so_14539884 = new Universal_Template();
And in the plugin folder, a file named 404.php:
<?php
/**
* The template for displaying 404 pages (Not Found).
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
MY 404!
</div><!-- #primary -->
<?php get_footer(); ?>

The solution depends on the content of 404.php file. If this file contains static text, like
_e( 'It seems we can’t find what you’re looking for...', 'twentyeleven' );
you can add your own filter
apply_filters( 'my_404_content', 'Default 404 message' );
and in functions.php (or in plugin)
add_filter( 'my_404_content', 'replace_404_message' );
function replace_404_message($message) {
return 'Error 404 - '.$message;
}
If 404.php uses built-in WP functions to display page content, you should check what filters they are supported.

You might be able to add a the_content filter with a conditional is_404 section:
function content_404($content) {
if (is_404()) {
// do some stuff with $content
}
// no matter what,
return $content;
}
add_filter( 'the_content', 'content_404' );
Note that this does assume that the 404.php page template has a the_content template tag in place.

Related

how to add stylesheet to my custom template file

I have created a new template for my wordpress page.here , the stylesheet i am adding is not reflecting on the page. how can i do that??
<?php
/*
* Template Name: Home Template
*/
get_header();
add_action('wp_enqueue_scripts', 'add_css_js');
function add_css_js(){
wp_enqueue_style('homepage', get_stylesheet_directory_uri() . '/styles/homepage.css');
}
function template_format_home($classes) {
$classes[] = 'home';
return $classes;
}
add_filter( 'body_class', 'template_format_home' );
?>
add this code wp_enqueue_style('style', get_template_directory_uri().'/styles/homepage.css', false); to your functions.php file and delete the following code
add_action('wp_enqueue_scripts', 'add_css_js');
function add_css_js(){
wp_enqueue_style('homepage', get_stylesheet_directory_uri() . '/styles/homepage.css');
}
from your home page. I think it works that way

Use a template file from a plugin in WordPress

I try to develop a plugin which includes a file from a template. At this moment my code view is like this:
/* Add a query var for template select, and and endpoint that sets that query var */
add_action( 'init', 'wpse22543_rewrite_system' );
function wpse22543_rewrite_system() {
global $wp, $wp_rewrite;
$wp->add_query_var( 'template' );
add_rewrite_endpoint( 'kalkulator_leasingowy', EP_ROOT );
$wp_rewrite->add_rule( '^/kalkulator_leasingowy/?$',
'index.php?template=kalkulator_leasingowy', 'bottom' );
$wp_rewrite->flush_rules();
}
/* Handle template redirect according the template being queried. */
add_action( 'template_redirect', 'wpse22543_select_template' );
function wpse22543_select_template() {
global $wp;
$template = $wp->query_vars;
if ( array_key_exists( 'template', $template ) &&
'kalkulator_leasingowy' == $template['template'] ) {
global $wp_query;
$wp_query->set( 'is_404', false );
include( get_stylesheet_directory().'/kalkulator_leasingowy.php' );
exit;
}
}
function prefix_movie_rewrite_rule() {
add_rewrite_rule( 'kalkulator_leasingowy', 'index.php?template=kalkulator_leasingowy', 'top' );
}
add_action( 'init', 'prefix_movie_rewrite_rule' );
This code runs very fine and includes the template file, but my template (header.php and footer.php) by default uses a Visual Composer and when I use this code on a page, view this:
Visual Composer works good on all pages without a /kalkulator_leasingowy.
How I can include a VC into /kalkulator_leasingowy as well?
File kalkulator_leasingowy.php
<?php
get_header();
?>
<div class="main-wrapper">
<div class="container ">
<div class="row">
</div>
</div>
</div>
<?php get_footer(); ?>
I'm not really understanding where you are trying to render custom Visual Composer code since your template file doesn't have any in it.
But based on your edit, it looks like you might actually want to be using a child theme. These make it very easy to add new template files to the parent theme without editing any of the parent's code and eliminate the need for most of your complex code.
If perhaps you are injecting the Visual Composer code from somewhere else, make sure you are applying the content filters rather than just inserting or echoing to the front end.
$content = '<div id="my_custom_content">[vc shortcode contents here]</div>';
echo apply_filters('the_content', $content);
This will make sure the end content is filtered and rendered appropriately. You might read this related answer for more information.

Rewrite Rule for custom template in WordPress is not working

I am trying to make a custom URL like "mysite.com/artist/bruno-mars" with a custom template "artist.php", which is in the theme folder.
Here is the "artist.php" template code for example
<?php
get_header();
//my custom code goes here //
get_footer();
?>
And now in the head of the theme functions.php, I put the code
/**
* Custom rewrite
*/
add_action( 'init', 'ic_rewrite_rule' );
function ic_rewrite_rule() {
add_rewrite_rule( 'artist/([^&]+)', 'index.php?artist=$matches[1]', 'top' );
}
/**
* Custom query vars
*/
add_filter( 'query_vars', 'ic_register_query_var' );
function ic_register_query_var( $vars ) {
$vars[] = 'artist';
return $vars;
}
/**
* Custom page template
*/
add_action( 'template_redirect', 'ic_url_rewrite_templates' );
function ic_url_rewrite_templates() {
if ( get_query_var( 'artist' ) )
add_filter( 'template_include', function() { return get_template_directory() . '/artist.php'; });
}
But this is not working. It is showing 404 error. Can anybody help me out.
Here is an example - Custom rewrite rules in Wordpress but not sure, If that might work.
UPDATE
I figured it out. Here is code for functions.php in theme folder.
add_filter('query_vars', 'add_artistname_var', 0, 1);
function add_artistname_var($vars){
$vars[] = 'artistname';
return $vars;
}
add_rewrite_rule('^artist/([^/]+)/?$','index.php?pagename=artist&artistname=$matches[1]','top');

Show Stylesheet only on Plugin page WordPress

I have a lot of CSS scripts in my WordPress plugin which may affect the other WordPress tags, such as form as an example:
.form {
padding:30px;
background-color:#fff;
}
I can change the CSS, but I want to have a script which only allows the stylesheet to show on the plugin page.
So to clear it up, currently the stylesheet <link> is always in the source code of the admin panel, but I want a script which only puts the stylesheet <link> in the source code when the user is on the plugin page.
You can use get_current_screen:
$screen = get_current_screen();
if ( $screen->id == 'your_plugin_page' ) ){
$custom_css = ".form {.....}";
wp_add_inline_style( 'your_main_style_handle', $custom_css );
}
Update 1:
To add a submenu to your admin menu:
add_action( 'admin_menu', 'your_plugin_admin_menu' );
//Add this to register you styles
add_action( 'admin_init', 'your_plugin_admin_init' );
then:
/**
* Register your stylesheet.
*/
function wpdocs_plugin_admin_init() {
wp_register_style('your-style', plugins_url('scripts/jquery-ui.css',__FILE__ ));
wp_register_script( 'jquery-ui', plugins_url('scripts/jquery-ui.js',__FILE__ ));
}
/**
* Register your plugin page and hook stylesheet loading.
*/
function your_plugin_admin_menu() {
$page = add_submenu_page(...., 'your_plugin_manage_menu' );
//Call 'your_plugin_admin_styles' only on the plugin’s options page
add_action( "admin_print_styles-{$page}", 'your_plugin_admin_styles');
}
/**
* Enqueue our stylesheet.
*/
function your_plugin_admin_styles() {
wp_enqueue_style('your-style');
wp_enqueue_script('jquery-ui');
$custom_css = ".form {.....}";
wp_add_inline_style( 'your-style', $custom_css );
}
/**
* Output our admin page.
*/
function your_plugin_manage_menu() {
// ...
}
you can create style-sheet, and create hook for that style-sheet reference. Then you can add WordPress using add_action(), within plugin call, where you like to call that script. So it will be referenced at head of page for on specified page as needed.
Read more on
https://developer.wordpress.org/reference/functions/wp_enqueue_script/
https://developer.wordpress.org/reference/functions/add_action/

Developing Wordpress-Plugin: Problems with loading .js into head and HTML-Snippet as a Shortcode

I was asked to develope a Wordpress plugin for a project I am currently involved in, since I normally do Graphics and UX Design (CSS3). I am not that familiar with developing plugins for WP, although I've got quite some understanding of PHP. I've got the following code:
<?php
/**
* Plugin Name: ExpButton Wordpress Plugin
* Plugin URI: https://url.de
* Description: Ein Generator des Expbuttons
* Version: 0.1
* Author: Wilko Meyer
* Author URI: http://url.com
**/
/**
* Loading js into header
**/
function add_async($url)
{
if (strpos($url, '#asyncload')===false)
return $url;
else if (is_admin())
return str_replace('#asyncload', '', $url);
else
return str_replace('#asyncload', '', $url)."' async='async";
}
add_filter('clean_url', 'add_async', 11, 1);
function expertbuttonjs()
{
// Loading the JS
wp_register_script( 'custom-script', plugins_url( 'https://www.expert-button.de/js.js#asyncload', __FILE__ ), array( 'jquery', 'jquery-ui-core' ), '20120208', true );
wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'wptuts_scripts_with_the_lot' );
/**
*Creating Shortcode
**/
add_shortcode( 'shortcode', 'expbutton' );
function expbutton( $atts ) {
/* Turn on buffering */
ob_start(); ?>
<div style="overflow:hidden;font-size:9px;height:auto;width:auto;text-align:center;margin:auto;" id="expertbuttonbg"><a target="_bl$
<?php
/* Get the buffered content into a var */
$sc = ob_get_contents();
/**
*Expbutton to Shortcode
**/
add_shortcode( 'shortcode', 'expertbutton' );
function expertbutton( $atts ) {
ob_start(); ?>
<div style="overflow:hidden;font-size:9px;height:auto;width:auto;text-align:center;margin:auto;" id="expertbuttonbg"><a target="_bl$
<?php
$sc = ob_get_contents();
ob_end_clean();
/* Return the content as usual */
return $sc;
}
?>
The purpose of this plugin is to load the js.js as async into the of the page and to create an shortcode from the HTML Code below the /* Turn on buffering */ section which can be used at wordpress sites.
Currently the code does not load the js into header and the shortcode which should be created does not work either and I have no Idea why. Hope someone can help me with this problem & has some clue.
Why the plugin doesn't work?
Thanks in advance.
I have made some important littles changes. The html code of your buttons is incomplete (so I have try to guess). The name ou your javascript file seems to me very strange and unusual...
<?php
/**
* Plugin Name: ExpButton Wordpress Plugin
* Plugin URI: https://url.de
* Description: Ein Generator des Expbuttons
* Version: 0.1
* Author: Wilko Meyer
* Author URI: http://url.com
**/
/**
* Loading js into header
**/
function add_async($url)
{
if ( strpos($url, '#asyncload') === false )
return $url;
else if ( is_admin() )
return str_replace('#asyncload', '', $url);
else
return str_replace('#asyncload', '', $url)."' async='async";
}
add_filter('clean_url', 'add_async', 11, 1);
There was a problem in the url in wp_register_script() function that I have corrected and in the add_action() too: your javascript file has to be inside your plugin root folder (I mean not in a subfolder). Also your JS file name seems to me very strange (normally it's file finishing with .js extension and not with .js#asyncload):
function expertbuttonjs()
{
// Loading the JS
wp_register_script( 'custom-script', plugins_url('/js.js#asyncload', __FILE__ ), array( 'jquery', 'jquery-ui-core' ), '20120208', true );
wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'expertbuttonjs' );
Dont use shortcode name for shortcodes functions. The code of your buttons are incomplete in your code. Also you can't have 2 times the same name for shortcodes or functions:
/*
* expbutton Shortcodes
*/
add_shortcode( 'expbutton', 'expbutton' );
function expbutton( $atts ) {
/* Turn on buffering */
ob_start(); ?>
<div style="overflow:hidden;font-size:9px;height:auto;width:auto;text-align:center;margin:auto;" id="expertbuttonbg"><a target="_blank" href="'.$sc.'">Text Button</a><div>
<?php
/* Get the buffered content into a var */
$sc = ob_get_contents();
/*
* expertbutton Shortcode
*/
add_shortcode( 'expertbutton', 'expertbutton' );
function expertbutton( $atts ) {
ob_start(); ?>
<div style="overflow:hidden;font-size:9px;height:auto;width:auto;text-align:center;margin:auto;" id="expertbuttonbg"><a target="_blank" href="'.$sc.'">Text Button</a><div>
<?php
$sc = ob_get_contents();
ob_end_clean();
/* Return the content as usual */
return $sc;
}
?>
This should work now.

Categories