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.