Adding Widget to Wordpress Admin Post/Page Edit Sidebar - php

I'm trying to find the proper way to add a custom widget to the sidebar on the post/page editor page, specifically, in the sidebar under the 'Page Attributes' section. I've been searching various keywords and coming up short. The Wordpress codex docs are limited, so I'm having any luck there either.
If there is a hook that can be used, what is it?
Else, the Wordpress functions needed to add the new control.
I'm guessing that the meta boxes are only displayed under the post section, so that wouldn't be the area I'm looking to edit.
Also, would using the Widget class be the wrong thing to do?
I'm developing for version 2.8 or higher.

You should pose that question on https://wordpress.stackexchange.com/
In short: Widgets on the editor pages are not widgets, don't use the frontend widget class for them. They are functions (callbacks) you need to register for the editor pages. You can differ between post and page and in later WP versions between the post type as well.
The function to add the meta box to the editor pages is: add_meta_box

Related

Edit the Wordpress sidebar with personalized content

I am creating a plugin for Wordpress. I'm in doubt which hook I need to call to add custom content to the Wordpress side menu bar.
I want to add a widget with a chart over there.
I already researched, but I didn't find anything about it. It would be better if I didn't need to change the files in the sidebar but rather do everything for my plugin.
You're looking for two functions add_menu_page and add_submenu_page
Detailed instructions are here:
https://developer.wordpress.org/reference/functions/add_menu_page/
https://developer.wordpress.org/reference/functions/add_submenu_page/

Allow page text to be edited from inside the wordpress menu without hardcoding it

New to WordPress theme design, and my google searching is not returning what I am after.
I have a theme I have created and its fine, but the text is all hardcoded into the theme.
I want there to be options in the theme appearance settings? (right place?) where a user can e.g. enter their 'about us' text.
But I have no idea what I should be searching or how to pull in that information into my theme.
Im no a beginner to PHP, but just the way WordPress works itself.
A link, search term or quick start is all I need.
I think you might find the (Free) Advanced Custom Fields plugin useful: http://www.advancedcustomfields.com/
You can add unlimited custom fields and create user interfaces for non-technical people to add content to your theme without them touching any code.
If you set up a field named 'about_text', calling it in your theme would look like this:
<?php the_field('about_text'); ?>
First, you should learn how wordpress is working to display the content. In back office, under Settings > Reading you'll find an option that let you tell how you want Wordpress to deal with the front page : is it a listing of the posts, or a static page?
If you choose the first options, it will use the index.php template from your theme folder. If it's a static page, you'll have to select which page to use for displaying the home (a page you created under the Menu page). The template that will be use then is front-page.php.
For every post / page (page is a post_type, just a variation of post) there will be a title field, a wysiwyg content and a featured image which will be displayed in a template. That's all you can manage by default. To display the title you can use the_title, the content the_content and for the featured image you will need the_post_thumbnail - note that those functions will need to be used inside the loop.
In order to display some more fun, you have many tools at your disposal:
Widgets : widgets are displayed in a sidebar - don't take it literally, it's just a zone of your template. You can register a sidebar with register_sidebar (use this inside your functions.php file, within an init hook). Then in your template you can display the sidebar with dynamic_sidebar.
In WP back office, go then under Appearence > Widgets. You will find your brand new sidebar where you can put any kind of widget you want. If you need a wysiwyg widget, I recommend you to install the Black Studio TinyMCE widget.
Custom fields : any post_type (a post type is an entry in wp_posts) in Wordpress have some associated metas store in wp_postmeta. A meta is defined by a key and a value - it's like any post have an associated array that you can customize.
There is two ways to work with custom fields. First, you can use the default Wordpress feature: when editing a page, click on the screen options button on the top right and enable "custom fields". You will now have a new area to work with on the bottom of the page : you can add fields by name (by default it will list the existing fields names but you can add your own) and value. It's good enough to simple text fields. Then in your template, you can get that value with the get_post_meta function.
The other way is to use a custom fields plugin, which allow you to have wysiwyg fields, loops, media uploader, datepicker... Two popular plugins are Advanced Custom Fields and Custom Field Suite. They both allow you to easily create set of fields for any page / post_type from the back office, and provide their own functions to manipulate fields in templates (but you can still use the WP functions if you want). For example, to get a custom field with Custom Field Suite you do: CFS()->get('my_custom_field').
Custom Post Types : sometimes, custom fields and widgets are not really convenient when you deal with a lot of data with possibly associated pages. In that cases, you can create your own post types just like posts or pages, but you can define which capabilities they have. Use register_post_type to define your new post type (still in init hook), that will make a new section available in your back office, just like posts. Then in your template you can query them with get_posts, the WP function that you will probably the most use when you'll start to play with Wordpress. I suggest you to read the WP_Query documentation in order to learn more about it.
And, at last but not least, you can create your own template that you can associate with any pages. For that, create a php file named whatever you want in your theme folder, paste into it the content from page.php and add this PHP comment on top :
/*
Template Name: My Template
*/
Then choose this template in the dropdown when you edit a page (on the right sidebar).
I think you will have enough to play with, but if you want learn more I recommend you to read about Wordpress hooks and the Widget API to create your own widgets.
Have fun with Wordpress!

How to add a widget by its shortcode on Wordpress posts / pages?

Is there any way we can assign a shortcode to already created widget and later use that
shortcode in our specific posts and pages to show that widget instead of trivial method of
showing widgets in sidebar ? I googled about this stuff didn't find anything relative.
Any suggestion would be welcome!
Yes, You can call widget in Post/Page using shortcode. Follow this link to establish a system like that.
http://digwp.com/2010/04/call-widget-with-shortcode/
This might be relevant.
Widgets on Pages
https://wordpress.org/plugins/widgets-on-pages/
Observations and two questions:
This plugin allows insertion of 1 or "sets" of widgets via shortcode into pages, posts, (and Custom post types?) and it will also save a list of configured shortcodes~sets. VERY handy!
When comparing its features to the plugin Widgets Shortcode, that plugin does not provide a means to save "sets" of plugins, rather it seems to focus on inserting widgets individually. In fact, it inserts the shortcode for each widget in the bottom edge of each widget and it assigns different shortcodes to each instance of a widget. Also nice.
https://wordpress.org/plugins/widget-shortcode/
I like the granularity Widgets Shortcode provides with an individual shortcode for every instance of every widget. When running it and your plugin simultaneously even widgets saved in your plugin's "sets" are still assigned individual shortcodes. Those individual shortcodes allow using a specific widget out of a "set" in a different location without needing to create an additional instance of the widget as it appears would be required with your plugin.
Incidently, for anyone who wants both of these features, these two plugins play very nicely together on one of our WP 4.0 Multisite installations with 50+ sites and even on a site using the theme Make.
FYI: We also tested amr shortcode any widget, but had some issues with it and rather than troubleshoot it we moved on. To be fair, we were testing quickly on a site with over 160 plugins active. Yes, 160 plugins. I know, I know, but think about it for a minute. Any plugin that you can activate into that mix of 160 plugins and it does not break things or introduce issues, then it suggests a good probability of it being troublefree to use.... well at least until the next round of WP updates. :)
We also tested Widgetize Pages Light and its pro version Sidebar & Widget Manager and had no issues with either. However, if only seeking to insert widgets into pages and posts these plugins seem like overkill as they provide additional page layout functions that may duplicate theme functions and/or other plugin functions.
I was looking for a "widgets in pages" plugin that provides a TinyMCE button to insert the widget shortcodes.
Can anyone recommend one that has a TinyMCE button to insert the widget shortcodes in pages, posts and CPTs?
Ultimately I selected Widgets On Pages as having the best combination of features for this application. Its simple to understand, easy to use, versatile and has worked flawlessly for us, even along side Widgets Shortcode.
You can do it by wordpress WordPress Widgets Shortcode plugin, you can Embed any widget area/dynamic sidebar to your pages/posts using this plugin as a shortcode
edit widgets in wp :
file in # wp-includes/widgets.php: register_widget()
in source view sample :
public function register( $widget_class ) {
$this->widgets[$widget_class] = new $widget_class();
}

php code for each wordpress widget?

I'm currently changing my theme and I need each individual code of each widget.
For example I need the rss widget, or categories widget or comment widget, etc.
I want those widget integrated in my footer.
Where can I see the code? I have no idea where to begin look...
I'd recommend creating multiple sidebar regions in your footer. You can make as many as you like, so just make one for each widget you'd like to have in the footer.
This way, you benefit from updates to the core widgets when WordPress tweaks them.

WordPress: Assigning widgets to individual pages

Are there any plugins or hacks that allow assigning widgets to individual pages?
EDIT:
Using self-hosted (.org) 2.8.4
An example of use would be: when you're editing a page in the admin, you'd ideally have the ability to assign widgets to that specific page. The default WP behavior is more "all or nothing" in that you assign widgets to the global sidebar and that's it... all pages get the exact same set of widgets... no "per page" assignment of widgets.
The Widget Logic plugin essentially achieves the desired result with a backwards approach, but it doesn't seem to work with the Rich Text Widget plugin.
There are several good ones. Display Widgets is my favorite. Widget Locationizer is perhaps more robust, but takes longer to manage. You can't go wrong with either one.
Finally found the Widget Context plugin which does what I need.
I don't know of any plugins that would give this functionality, but you might want to look at page templates here: WP Page Templates
With this route its more of a hack, as you can't use the CMS backend to manage your widgets. You would have to manually cut and paste the code of the widget into your page.
How about WP Page Widget:
Select widgets for each page / post / custom post type. For every
single page / post / custom post type we can select which widgets to
show.
This does it "the right way", i.e. when you edit a page you can choose which widgets to show on that page.

Categories