How can i make for example Worpdpress Widget.
I want a area dragged and dropped and hold its position into the database.
Does somebody has tips/ideas?
Thanks!
Create a new plugin and open init.php file.
add following code(this is an example code for a widget)
<?php
/*
Plugin Name: Example: My User Widget
Description: This plugin provides a simple widget that shows the name of
the logged in user
*/
class My_User_Widget extends WP_Widget {
function My_User_Widget() {
parent::WP_Widget(false,’My User Widget’);
}
function widget($args) {
$user=wp_get_current_user();
if(!isset($user->user_nicename)) {
$message=’Welcome Guest’;
}
else {
$message=”You are logged in as {$user->user_nicename}”;
}
extract($args);
echo $before_widget;
echo “<p>$message</p>”;
echo $after_widget;
}
}
function register_my_user_widget() {
register_widget(‘My_User_Widget’);
}
add_action(‘widgets_init’,’register_my_user_widget’);
widgets_init hook will call register_my_user_widget method. Then My_User_Widget Class will be called. once you activated the widget you can see it in widgets window and drag and drop it where you want.
Related
I read carefully How to display notice in admin panel on Plugin Activation? which is similar, but I could not make it work correctly
class Shoutbox {
function display_notice() {
echo '<div class="updated">
<p>I am a little yellow notice.</p>
</div>';
}
public static function pluginActivated() {
//exit("Plugin has been actived"); // this is displayed when not commented
add_action('admin_notices','display_notice'); // this notice is never displayed.
}
}
//add_action('admin_notices', array('Shoutbox', 'display_notice')); // this is displayed when not commented
new Shoutbox();
I also tried with
public static function display_notice() {
Any idea on how to display the admin notice inside pluginActivated() ?
You need to pass the method to the add_action function as an array, with the first element being the class name, and the second element being the method name. you can follow my code :
class Shoutbox {
public static function display_notice() {
echo '<div class="updated">
<p>I am a little yellow notice.</p>
</div>';
}
public static function pluginActivated() {
add_action('admin_notices', array(__CLASS__, 'display_notice'));
}
}
new Shoutbox();
I have to add a drag and drop feature on wordpress media screen : wp-admin/upload.php
class myClass extends Config {
public function __construct() {
add_action('admin_enqueue_scripts', [$this, 'callback_admin_enqueue_scripts'] );
}
// Enqueue scripts
public function callback_admin_enqueue_scripts() {
wp_register_script('myscript', MY_BASE_URL . '/scripts/my-script.js', array('jquery','jquery-ui-droppable','jquery-ui-draggable', 'jquery-ui-sortable'), '1.0' );
}
}
And later from my-script.js :
jQuery(document).ready(function($){
// Where .attachment.save-ready is a class of each attachment
console.log(jQuery('.attachment.save-ready'));
});
And the result from the console is just null, seems the attachment is loaded after my script.
Someone has already seen similar issue? what is the recommended way to append scripts to worpdress media screen.
Thank you
If you face script loading issue then you can also use setTimeout function to call script after 2 sec once DOM ready.
I am trying to add a button in wordpress register form but the position of button is not right as i want. It is looking like the below image
But i want it something like this
Given below is my code
public function init()
{
global $mySetting;
$mySetting = get_option('my_options');
add_action('register_form', [$this, 'my_registration_button']);
add_action('login_form', [$this, 'my_registration_button']);
}
public function my_registration_button()
{
?>
<p>My Button</p>
<?php
}
I have a WordPress site. There are many users. I want when an author logs in, the author who currently is currently logged on couldn't access the "edit page" menu in the admin bar.
Is there any plugin to disable that?
You can use this plugin :
http://wordpress.org/extend/plugins/admin-bar-disabler/
OR Alternative and manual way is under if condition place this
show_admin_bar(false);
E.g.
if(!is_admin())
{
show_admin_bar(false);
}
place this code in functions.php so that it will disable the admin bar for all the other users.
In your functions.php file, you can add one of the following code snippets to get the indicated results:
// Only display to administrators
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
// Disable for specific role (in this case, 'subscriber')
function remove_admin_bar() {
$user = wp_get_current_user();
if (in_array(‘subscriber’, $user->roles)) {
show_admin_bar(false);
}
}
Following is my code
$op=array("description"=>"Ads Widget");
wp_register_sidebar_widget('adswidget','Ads','ads_widget',$op);
register_widget_control('adswidget','ads_widget_control');
I can use only 1 Ads Widget. I want to use more than 1 Ads Widget ? How to write it ? I'm finding in google and still not found.
Still not found document on
http://codex.wordpress.org/Function_Reference/wp_register_sidebar_widget
also.
By default all widgets that are created using the widgets api are multi instance.
The code you have above is the old method before WordPress 2.8. Now you just need to extend the widget class and add some functions. Default Example:
class My_Widget extends WP_Widget {
function My_Widget() {
// widget actual processes
}
function form($instance) {
// outputs the options form on admin
}
function update($new_instance, $old_instance) {
// processes widget options to be saved
}
function widget($args, $instance) {
// outputs the content of the widget
}
}
register_widget('My_Widget');
See Codex Page: http://codex.wordpress.org/Plugins/WordPress_Widgets_Api
I like the solution above as it's much simpler and easier to implement, but here is another method of multi-widgets which sort of provides a behind the scenes look as well...
http://justcoded.com/article/wordpress-multi-widgets/