How to convert a Shortcode into a Plugin - php

1. Shortcode (working)
I have made a Shortcode which display the current weatherinformations from yahoo. There was 3 Files (simple-pie.inc, simple-pie-yahoo.inc and yahooweather.php) and I grabed the content of these files and pasted it inside my function.php – works good!
This looks so and got more then 16.000 Lines of Code:
// Add Shortcode for weather
function get_rohrau_wetter() {
content of simple-pie.inc
content of simple-pie-yahoo.inc
content of yahooweather.php -> return "weather-html"
}
add_shortcode( 'rohrauwetter', 'get_rohrau_wetter' );
2. Plugin (no clue where to start)
All I want to do is making a Plugin out of my working shortcode to get that "wall-of-code" out of my function.php and of course for better reuseability. Also I think so it will be possible to turn the simple-pie cache to "on" for faster processing…
My Question is: How do I convert my Shortcode into an Plugin?

A good place to start would the plugin handbook: https://developer.wordpress.org/plugins/the-basics/header-requirements/
But the cheap and nasty way to place it into a plugin, create a directory for your plugin, create a index.php (realistically it can be any name) file. Add this in
<?php
/*
Plugin Name: WordPress.org Plugin
Plugin URI: https://developer.wordpress.org/plugins/the-basics/
Description: Basic WordPress Plugin Header Comment
Version: 20160911
Author: WordPress.org
Author URI: https://developer.wordpress.org/
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: wporg
Domain Path: /languages
*/
Then underneath it add in your above functions. Obviously Change out the text to suit yourself so that you can identify the plugin. Then upload it too your plugins directory.
As I said, this is the cheap and nasty way to set it up. I would advise to have a look at the plugin handbook I linked to above. Also Using OOP plugin development is advised, a good starting point is Tom McFarlin tutorial on tutsplus: https://code.tutsplus.com/series/object-oriented-programming-in-wordpress--cms-699 .. and after that well worth looking at https://github.com/DevinVinson/WordPress-Plugin-Boilerplate and https://wppb.me/ hope that all helps.

Related

WordPress independent functions.php

I have a fresh WordPress and bbPress installed on an internal server.
While I was setting up bbPress I wanted to test the functionalities like creating a forum, topic, etc. When doing these stuff in the back-end (dashboard) there didn't seem to be any problems but when I did it from the front-end I kept getting
ERROR: Are you sure you wanted to do that?
I searched for a solution and found this.
add_filter( 'bbp_verify_nonce_request_url', 'my_bbp_verify_nonce_request_url', 999, 1 );
function my_bbp_verify_nonce_request_url( $requested_url )
{
return 'http://localhost:8888' . $_SERVER['REQUEST_URI'];
}
I simply changed the hard-coded URL to what our internal server is set up to and it fixed the problem.
Now my question: is it possible for me to add this solution to a functions.php that is independent of the theme being used? I asked this because I have 2 concerns:
The current theme might get updated and will overwrite the file
I'm aware that the solution to this is simply create a child theme but my second concern prevents me from doing this.
The WordPress administrator might change themes and so both the functions.php file on the main theme and the child theme will stop working
How could I add the solution above so that I don't have to worry about the theme being updated and/or replaced with a new theme in the future? I don't want to keep adding this solution every time the administrator decides to change themes.
If you can't put it in a theme, put it in a Plugin. If you're worried that the plugin will get de-activated make it a Must Use Plugin.
It's dead simple to create a plugin. Create a file plugin-name.php and place it in a directory wp-content/plugins/plugin-name/. That file should contain the following code:
<?php
/*
Plugin Name: Name Of The Plugin
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: The Plugin's Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
*/
add_filter( 'bbp_verify_nonce_request_url', 'my_bbp_verify_nonce_request_url', 999, 1 );
function my_bbp_verify_nonce_request_url( $requested_url )
{
return 'http://localhost:8888' . $_SERVER['REQUEST_URI'];
}
If you want it to be a must use plugin, put it in wp-content/mu-plugins/ instead of wp-content/plugins.

Unable to integrate downloaded HTML template in wordpress Theme

I am trying to Convert my HTML template into Wordpress template.But on browser it is only showing content without CSS. I refered the procedure from: https://www.elegantthemes.com/blog/tips-tricks/converting-html-sites-to-wordpress-sites
here I added a below given statement on the top of a CSS file.I am confused about what to write in Theme URI and Author URI, as Right now I am workin on localhost server.
/*
Theme Name: ThemeIntegration
Theme URI: ??
Description: A brief description.
Version: 1.0
Author: You
Author URI: ??
*/
I am only getting Simple html content without styling.
You need to add css and js files either in header or functions.php file, that tutorial is incomplete you can refer this link
If you have only 1 css then you can directly write it in style.css

Override a wordpress theme language file using a custom plugin

I am using a wordpress theme that contains several language files. I would like to modify a language file to "white label" the theme I am using. I will also be making other admin-css changes to the theme, therefore, I'd like to create a plugin that allows me to do all of this.
What I am unsure of how to do is create a custom plugin that would override a themes language file - is this possible?
I have so far tried this:
<?php
/*
Plugin Name: Layers Whitelabel test
Plugin URI: http://www.skizzar.com
Description: Plugin to whitelabel layers for Skizzar
Author: Skizzar
Version: 1.0
Author URI: http://www.skizzar.com
*/
function set_myplugins_languages() {
load_textdomain( 'layerswp', plugin_dir_url( __FILE__ ) . '/language' );
}
add_action('init', 'set_myplugins_languages');
But so far no joy - as in, it doesn't load the new language file in place of the existing one.
Anyone got any ideas on how this can be achieved?
Well, why do you need a plugin for this?
The theme style.css have similar code to
Plugin Name: Layers Whitelabel test
Plugin URI: http://www.skizzar.com
Description: Plugin to whitelabel layers for Skizzar
Author: Skizzar
Version: 1.0
Author URI: http://www.skizzar.com
So you can simply change theme display name, url, author etc.
Basically it's easy to do, however it's against theme copyrighting ;) Even if this is a GPL theme you should refer to original developer.

Translate WordPress plugin header

How can I translate WordPress plug-in header?
I have translate all strings in my plug-in using:
__() and _e() functions
.po files
Text Domain
WordPress function to load language file
load_plugin_textdomain('mnbaa-seo', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
I want to translate this section
<?php
/*
Plugin Name: Mnbaa SEO
Plugin URI: http://www.mnbaa.com
Description: WP blugin fom make SEO and Social SEO.
Author: Mnbaa CO
Author URI: http://www.mnbaa.com
Version: 1.0
Text Domian:mnbaa-seo
Domain Path: /languages/
*/
?>
You have to use WordPress' i18n tools to get the plugin's description translatable. i18n tools' makepot.php lets you auto-generate a .pot file, which then can be used to create the .mo/.po pair (with the free PoEdit for example).
The easiest way would be:
Download the zip from https://github.com/wp-mirrors/wp-i18n-tools.
Extract its content into the wp-includes folder.
Command line one-liner from within WP root: $ php wp-includes/makepot.php wp-plugin wp-content/plugins/MYPLUGIN/ wp-content/plugins/MYPLUGIN/MYLANGFOLDER/MYPLUGIN.pot
Then you take this resulting .pot file as source for PoEdit.
Finally ensure to name the .mo/.po like your plugin plus language code, for example: MYPLUGIN-de_DE.mo/MYPLUGIN-de_DE.po.
More (but not quite up-to-date) info can be found at WPO: https://codex.wordpress.org/I18n_for_WordPress_Developers#Translating_Plugins_and_Themes
I wonder if maybe PoEdit Pro can do that without us calling on the command line... Maybe someone who knows can drop a comment below.
Add somewhere inside your plugin code a dummy translation for the description and plugin name:
$dummy_name = __( 'Mnbaa SEO', 'mnbaa-seo' );
$dummy_desc = __( 'WP blugin fom make SEO and Social SEO.', 'mnbaa-seo' );
Then re-generate the .po/.mo pair.
I use only /languages for Domain Path, no trailing slash.
Check this answer for I18n support by plugins.

How Does Wordpress Read Comment Title?

I'm working on my own little back end framework for future clients and I'm making a folder named "Forms" to store all of my forms for CMS interaction. Inside the forms folder I'd like to store .php files similar to wordpress style plugins that I can title in the comments code like wp does... ie
/*
Plugin Name: Name Of The Plugin
Description: A brief description of the Plugin.
Version: The Plugin's Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
*/
I'm wondering how WP reads that data as it is commented out... do they use file_get_contents('plugin.php'); and parse it as well as include the plugin? Is there a php function to actually read comments?
Thoughts?
Read the file by FTP or file_Get_contents()`
explode each line by : and get result in array
array[0] makes the key and array[1] makes the value.
Wordpress has created its own function to handle file headers. I used this in my new Total Widget Control Plugin. Here's the function name and help docs on how to use it.
get_file_data( $file_path, $headers, $context)
http://codex.wordpress.org/File_Header
string $file: Path to the file
array $default_headers: List of headers, in the format array('HeaderKey' => 'Header Name')
string $context: If specified adds filter hook "extra_{$context}_headers"

Categories