Finding error in my WordPress plugin template - wordpress

I generated a WordPress plugin. However: When I include css and js file I get an error when I edit a page where I added the shortcode.
So what I do: I create a page, add the shortcode, it loads the plugin and my Hello World text. But then, when I save the edited page, it shows the error, hat there were errors during saving and this could be up to server limitations. But I am pretty sure, it has nothing to do with that. Also it does not matter what theme or builder I use. (Mostly I use Divi)
So the question is: Do you see any error in my plugin code? In folder plugins I have a folder called "myPlugin" containing a main.php containing this code:
<?php
/*
* Plugin Name: My Plugin
* Plugin URI: https://example.com
* Description: A brief description of my plugin
* Version: 1.0.0
* Author: Your Name
* Author URI: https://example.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: my-plugin
* Domain Path: /languages
*/
function my_shortcode_function() {
echo "<p class='myxxx'>Hello world</p>";
}
add_shortcode('my_shortcode', 'my_shortcode_function');
function my_plugin_init() {
wp_enqueue_style('my-plugin-style', plugins_url('css/style.css', __FILE__));
wp_enqueue_script('my-plugin-script', plugins_url('js/script.js', __FILE__));
}
add_action('wp', 'my_plugin_init');
The css file styles.css contains only:
/* css file */
.myxxx { color:red; }
Very important: On lets say page /test I put [my_shortcode] to run the plugin. Works. It shows in red "Hello World". BUT when I save its giving the saving error. (But only when I enqueue the css and js.) Then I can load the page /test in non-builder mode and the see the plugin loaded and showing Hello World also. So it works but I get the saving error. I really need to get rid of it.
Any ideas?
Thx
CC
I tried many template also also used a new generated template using chat gpt. ;D
But I tried a lot how to integrate the css and js.

Related

Hook In Wordpress

I modify wp_hash_password and wp_check_password with my own encryption password, I want to use hook now. This is to make sure when my wordpress update the latest version, my wp_hash_password and wp_check_password in pluggable.php still is my own password encryption, the hook should add at where? Look on Internet, some say put in user.php and some say put in function.php in theme. If who know, please tell me the answer.
Plugins are loaded first, then pluggable.php and then lastly the theme. Subsequently, you need to create a plugin and place your pluggable functions code in there otherwise your custom code won't be loaded. Create a file called custom_wp_password_override.php or whatever you want to label it and place it in your plugin's folder adding in your own custom function code. You should never update core files such as user.php as this will be overwritten when you upgrade WordPress.
<?php
/*
Plugin Name: Custom WordPress Passwords
Plugin URI: http://localhost
Description: Override wordpress pluggable password functions.
Version: 1.0.0
Author: Your Name
Author URI: http://locahost
Text Domain: custom-wp-passwords
*/
if ( !function_exists('wp_check_password') ) :
function wp_check_password($password, $hash, $user_id = '') {
// Your custom code in here
}
endif;
if ( !function_exists('wp_hash_password') ) :
function wp_hash_password($password) {
// Your custom code here
}
endif;
?>
So the first thing I know when into WordPress development is not to edit any files in two folders: wp-include and wp-admin. Or you will lose all your edit when update WordPress to the newer version.
Back to your question, if you think you will not change the theme in the future, put your code in child theme's functions.php. Yeah, child theme will not affect when you update your parent theme.
And I think, the best solution for you is creating a simple WordPress plugin, put your code in, and activate that plugin. So your encryption will not lose when you update WordPress core/theme/other plugins even.
<?php
/*
Plugin Name: WP Custom Plugins
Plugin URI: http://link to your plugin homepage
Description: This plugin changes WordPress hashing password encryption.
Version: 1.0
Author: Someone
Author URI: http://link to your website
License: GPL2 etc
License URI: https://link to your plugin license
/* Your code goes below here */

Wordpress child theme not loading modified template PHP files inside subfolders

I'm starting learning Wordpress development, and I'm stuck trying to create a child theme. Below I enumerate the steps I followed to see if someone can help me:
Create a folder under 'wp-contents/themes' called 'shoptest'
Inside, I've created a file 'style.css' with the following content:
/* Theme Name: Shoptest
Theme URI: http://www.testsite.com
Description: Shop Isle child theme
Author: Felip
Template: shop-isle
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: light, dark, two-columns, right-sidebar, responsive-layout, accessibility-ready
Text Domain: shop-isle
*/
I created a file 'functions.php' with the following:
I've activated the theme in Wordpress dashboard
I've created a couple of folders 'inc/structure' and copied the file original 'inc/structure/footer.php' inside.
So:
The original file is: 'wp-content/themes/shop-isle/inc/structure/footer.php'
The child file is: 'wp-content/themes/shoptest/inc/structure/footer.php'
In theory, if I change/add something in the child file, the webpage should reflect that change, right? I've tried but it does nothing. Just for testing, if I change anything on the original footer.php file, it's immediately changed in the website.
What am I doing wrong?
Wordpress doesn't work this way as you think.
You should write your changes as a function and put it in functions.php of your child theme.
Then use the wp_footer hook to hook your changes to the footer.
like below
function my_custom_changes() {
// add your code
}
add_action('wp_footer', 'my_custom_changes' );

wordpress ajax for a plugin only works when defined in the active theme

I'm testing with very simple code to try and get ajax working from within a plugin I'm writing. However I always get the dreaded 0 returned from the admin-ajax.php file.
The basic code defined in my plugins main php file is:
// Init custom actions
add_action( 'wp_ajax_import_run', 'import_run' );
function import_run() {
echo "testing 123";
die();
}
And then import_run is the action parameter parsed via the jquery ajax call.
Now the funny thing is, this works fine when I place the above php code in the themes main function.php file, but whenever I place the code in the actual plugin where it's needed it won't work. The issue is it needs to be in the plugin, not the theme.
So it seems I'm missing some small vital step about where to put my add action and function within the plugin. Any ideas?
you haven't provided your complete code in your plugin.
You can't invoke plugin function using wp ajax call.
Here's a simple WP plugin code to display some text everywhere.
<?php
/**
* #package Testplugin
*/
/*
Plugin Name: Testplugin
Plugin URI: someuri
Description: To demo a simple plugin
Version: 2.5.8
Author: Automattic
Author URI: http://automattic.com/wordpress-plugins/
License: GPLv2 or later
*/
function pluginnamehere() {
echo "This gets displayed everywhere";
}
add_action( 'plugins_loaded', 'pluginnamehere' );
I've worked out what was going on. I was adding the add_action hook everywhere but in the core root plugin page. Previously I had been adding it to a secondary php file that I thought was the root plugin file but wasn't.
The confusion occurred because I'm using a boilerplate empty plugin template to work with, so even though the plugin does very little so far its actually full of code and remarks already.

Wordpress plugin get version. Am I doing this correctly?

I want to make a plugin for all of my own wordpress-based websites.
The idea is simple, I want to get the version (and probably another info) of those sites by using CURL.
Right now, I have such a code:
<?php
/*
Plugin Name: Manage Site
Plugin URI: http://not-available-yet.com/
Description: A manage site plugin
Author: Go Frendi Gunawan
Version: 0.0
Author URI: http://not-available-yet.com/
*/
// this file is located on wp-content/plugins/manage_site
require_once '../../../wp-includes/version.php'; // do I need to include everything manually like this?
if(isset($_GET['key']))
{
$key = $_GET['key'];
if($key=='1234'){
echo $wp_version;
}
}
And I can access it by using CURL with this address:
http://my_domain.com/wordpress/wp-content/plugins/manage_site/manage_site.php?key=1234
And I'll get the wordpress version as a response:
3.5.1
It is work for now.
But, since this is my first time writing a wordpress plugin, I might do things wrongly. So, Am I doing it right? Or is there any better way to write this?
I've found the answer.
Both, #kjetilh and #brasofilo was right in case of accessing wordpress plugin "the normal way".
Since I access the plugin directly, the wordpress bootstrap is not loaded. Therefore, my first approach was partially right.
But load the wp-load.php is more elegant (and simple and usable) since it load everything, not only the version, and it gonna be useful to access 'more information':
<?php
/*
Plugin Name: Manage Site
Plugin URI: http://not-available-yet.com/
Description: A manage site plugin
Author: Go Frendi Gunawan
Version: 0.0
Author URI: http://not-available-yet.com/
*/
// this file is located on wp-content/plugins/manage_site
require_once '../../../wp-load.php';
if(isset($_GET['key']))
{
$key = $_GET['key'];
if($key=='1234'){
echo $wp_version;
}
}

Embedding Wordfaire into Self Hosted Wordpress

I am trying to get Wordfaire live blogging embed code to work on my site and having some problems. I created a new page for my live blog feed on site site and added the embed code to the html page in wordpress. When I test it out and try a test live blog nothing runs on the page.
Is there a trick to getting it to work on wordpress??
Thanks in advance to anyone that can assist!
It sounds to me that you are using the HTML view of the WordPress editor to paste the embed code, correct?
A better way would be to make a simple plugin that will create a shortcode with the embed code, then you use that shortcode to display the embed code on your page.
Example:
embed-shortcode-plugin.php
<?php
/*
Plugin Name: Plugin Name
Plugin URI: http://pluginurl.com
Description: Plugin description
Version: 1.0
Author: Author Name
Author URI: http://authorurl.com
*/
function embed_shortcode($atts,$content=null){
extract(shortcode_atts(array('optionname'=>'defaultvalue'),$atts));
// The extract() function above will allow you to do [shortcode optionname="defaultvalue"] in your pages and use $optionname to get the value below
// The extract() line is optional and can be removed
// $optionname = defaultvalue
return 'put your embed code here';
}
add_shortcode('shortcode','embed_shortcode');
// This will add the shortcode 'shortcode': [shortcode], change to whatever
?>
Put that in your plugins folder and then activate it. Alternatively, you could remove the stuff between the /* and */ and put that code right in your theme's functions.php file and it will run the same.

Resources