Hello and thank you for helping in advance,
I am trying to create a widget that lives inside the post.php page for when editing a post that exists (Not new-post.php, I need an ID). This custom widget is going to get the posts ID and shoot it to another database to store it, it’ll do this through ajax and have it’s own independent save button.
Pretty simple right? Except I can’t figure out how to get my widget to show on the post.php page.
Here is my test code in themes/myTheme/functions.php, just to try and get it in the right place…but it’s not working.
function artist_associator_dashboard_widget_function(){
echo "Hello world";
}
function artist_associator_add_dashboard_widget(){
wp_add_dashboard_widget('artist_associator_dashboard_widget', 'Artist Associator', 'artist_associator_dashboard_widget_function');
}
add_action('wp_dashboard_setup','artist_associator_add_dashboard_widget');
I also tried putting in a few other actions that looked promising but it wasn’t working out, couldn’t find my functions.
For one, Dashboard widgets (what you’re using in the example) are for the main wordpress admin page.
I think what you want is this tutorial. You also want to look at add_meta_box to look at how to add a metabox to your post screen. Here’s some code from my current project that should help you to add a metabox to the post edit screen.
Change all to suit your preferences. Basically this creates me a custom metabox that appears as the primary input selection for the post (I don’t use editor or title support). Context can either be “normal,” “advanced,” or “side.” I haven’t found a use for ‘advanced’ though it’s default for some reason..
Anyway, You’ll probably need to throw in a check to see whether you’re editing an existing page or you’re creating a new post.
Though, a better solution perhaps would be to use add_action to hook the
save_post
event and use your ajax there to save the post ID to your external database.