How To Update Featured Image Programatically in Gutenburg - wordpress

I thought I’d be able to update the featured image using this code…
wp.data.dispatch( 'core/editor' ).editPost( { featured_media: 10 } );
However, I get an error:
Uncaught (in promise) Error: The entity being edited (postType, undefined) does not have a loaded config.
What needs to be changed?

Have you tried this:
wp.data.select( 'core/editor' ).getEditedPostAttribute( 'featured_media' );
wp.data.dispatch( 'core/editor' ).editPost({ featured_media: 10 });

I had the same error show up in my browser console, when I was trying to make a custom post type editable through a custom-made block. In my case, it wasn't a problem with the gutenberg block itself (i.e. the js file or the registration of the block), since I could use the block in other post types without the error showing up in the console.
I would suggest checking the slugs/names of any taxonomies (and perhaps post meta fields) which are registered for your specific post type. I think I must have confused the Wordpress REST API as it was loading the post in the gutenberg editor because I had registered a reserved word as one of my taxonomies (I had a taxonomy with machine name "type"). I changed the taxonomy to "taxon_type" (hoping to avoid any name clashes), and the error went away!
I initially thought that it was a problem with a Wordpress API call that I was making in the block itself, but I believe the error message is signaling that the gutenberg editor is having trouble loading your post type for whatever reason.

Related

Completely custom page in WordPress generated by a plugin

I am writing a WordPress plugin that has an AJAX element to it - blocks of HTML are fetched by the front end from the plugin using AJAX.
I am having difficulty joining up the pieces here, and I suspect it is just a terminology issue. How would I implement a page completely provided by the plugin?
The content of the page will be HTML - the plugin can generate this in response to POST or GET parameters.
There needs to be a route to this page. The route does not have to be user-friendly or a REST style - just some kind of URI that gets to the plugin. Is there perhaps a way to register a custom page with an arbitrary name, without having to create it as a WP post?
I would like all this to be self-contained in the plugin, so should not involve the administrator having to create posts or pages, or have to add anything to the theme.
Ideally I would avoid any URLs that go into the wp-admin directory. End users don't belong in here.
I would strongly suggest referring to https://codex.wordpress.org/AJAX_in_Plugins#Ajax_on_the_Viewer-Facing_Side
You need to have a php script in your plugin directory that returns what you request, and you need to determine that url at run time for reference in your ajax. The above link gives an example for enqueuing and using wp_localize_script() to provide the url for your custom php script.
wp_enqueue_script( 'ajax-script',
plugins_url( '/js/my_query.js', __FILE__ ), array('jquery') );
// in JavaScript, object properties are accessed as
// ajax_object.ajax_url, ajax_object.we_value
wp_localize_script( 'ajax-script', 'ajax_object',
array( 'ajax_url' => plugins_url( '/php/myapi.php' ));
Your javascript file will be included on every page and will listen for events on the page which require a block of HTML, as you've described it.
Your file myapi.php then needs to take a request, probably using a query string, get the appropriate content from the wordpress api, and respond with said content, which your javascript will put into place.
To have access to the wordpress api as well though, you have two options:
Force wordpress to run starting with your file, by including wp-load.php. This is probably not the cleanest way.
Set up a custom page or slug to direct to your plugin.
I would advise the second option, and advise a slug, in which case you may find this post helpful: wp_rewrite in a WordPress Plugin
From Jason's comment, based on the link above:
The rewrite rules are mentioned a lot, but are really a distraction -
they just help to make URLs look more "friendly", which was not an
objective here. The key is: register a custom GET parameter; look for
that parameter early in the page rendering process; if you find the
parameter is set, then output/echo stuff and die(). There are a
number of hooks that can be used to look at the parameters, chosen
dependin on how much you want WP to set up and process first.

Custom Pods.io detail page

I am attempting to create a custom template page for a details page for my Pods.io 'research_faculty' pod.
I have the url going currently to /people/details and it does go to that page but I created a 'details' page that I have assigned a custom template to display the information that I want. Problem I am having is that its treating the last part of the URL as a page and Wordpress is saying page cannot be found. So it would be /people/details/wally-kolcz. How can I use the past part of the URL as the way to pull the person's details and populate the template page rather than WordPress trying to use it as another page and defaulting to the single.php template?
Pod (Advanced Options)
Custom Rewrite Slug: people/details
You can use pods_v( 'last, 'url' ); to get the last segment of the url. If you put that in a variable you can use it to build your Pods object.
// get current item
$slug = pods_v( 'last', 'url' );
// get pods object for current item
$pods = pods( 'pod_name', $slug );
See this tutorial for more information: http://pods.io/tutorials/using-pods-pages-advanced-content-types/
Um create a pods page and on the Enter page URL section enter /people/details/* so that it responds to /people/details/+ anything. Also in the slug section u can try putting {#url,2} If it doesnt work. Also go to the advanced options of your advanced content type and in the detailed page URL enter pod_name/{#permalink} though that may not be necessary but try that and test it again.Make sure the pods page also has a pod to refer to and that it points at your php details template

How to make your own post format in Wordpress?

How can I create my own custom post formats?
Or how can make my custom post type make work with a function like
get_post_format();
For example i have a custom-post type with the type of "accordion" and i like to be able to use it with as content element in the loop, but only if it exists...
get_template_part( 'content', get_post_format() );
So i am looking for a function like
get_custom_post_format();
which does not exists in Wordpress.
Anybody tried something similar?
I'm not sure if you're asking how to create custom post formats or custom post types so I've provided the answer to both.
If you're asking whether you can create custom post formats...
...then the answer is no. See the quote below from Post Formats on the WordPress codex:
The Post Formats feature provides a standardized list of formats that are available to all themes that support the feature. Themes are not required to support every format on the list. New formats cannot be introduced by themes or even plugins. The standardization of this list provides both compatibility between numerous themes and an avenue for external blogging tools to access this feature in a consistent fashion.
If you're asking how to create a custom post type:
The most basic example of creating (registering) your own custom post type is to add the following code to your functions.php file inside your theme.
function register_recipes_post_type() {
$args = array( 'public' => true, 'label' => 'Recipes' );
register_post_type( 'recipe', $args );
}
add_action( 'init', 'register_recipes_post_type' );
The above code hooks our register_recipes_post_type function to be executed when the init action is triggered by WordPress core.
Once you've added this code, if you go to your wp-admin you'll see a new menu on the left called 'Recipes', that's your new custom post type. If you add a new recipe, give it a title and some content, publish it and then try to preview it, you'll notice that you get a 404 error. After creating a new custom post type you need to go to your Settings > Permalinks in your wp-admin, just visiting that page will fix your permalinks to include the new custom post type so if you now go back and refresh the preview of the recipe you just created you'll see that it now works rather than 404s.
Now if you create a new file called single-recipe.php and put some code inside it, just put 'test' now for the purpose of seeing that it works and once you have, refresh the preview of the recipe you just created once again and you should see that it just displays the word 'test'. Using that file you can create a completely custom template to be displayed for showing single entries (posts) of that custom post type, or you could use content-recipe.php if your single.php includes a get_template_part( 'content', get_post_format() ); as you said in your original post.
Obviously your custom post type probably won't be for recipes but just update instances of recipe and recipes to whatever you want it to be.
There are also other post type specific templates you can create too for your archive of the post type etc. The above should be enough to get you started though.
There are also other arguments you can pass when registering your post type, you can see the full list here: http://codex.wordpress.org/Function_Reference/register_post_type
I hope this helps. Good luck! =)
Creating New post format is not allowed currently by WordPress. you can’t define any post format apart from what WordPress allows.
Reference:
1. http://wp.tutsplus.com/tutorials/proof-using-post-formats/

Why do I get a blank page on wordpress after saving a post (after installing global custom fields)

I just installed Global custom field just as shown here http://digwp.com/2009/09/global-custom-fields-take-two/
Basically I added this on my themes/themename/functions.php:
<?php
//Custom Theme Settings
add_action('admin_menu', 'add_gcf_interface');
function add_gcf_interface() {
add_options_page('Global Custom Fields', 'Global Custom Fields', '8', 'functions', 'editglobalcustomfields');
}
function editglobalcustomfields() {
// the html form (too long to bother you with)
}
?>
Now when I save posts I get a blank page. The post does get saved though.
Can you help in any way?
I get these two errors
Notice: has_cap was called with an argument that is deprecated since version 2.0! Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead. in/www/newBlogs/w.sandbox/wp-includes/functions.php on line 2722
Warning: Cannot modify header information - headers already sent by (output started at /www/newBlogs/w.sandbox/wp-content/themes/boilerplate/functions.php:548) in/www/newBlogs/w.sandbox/wp-includes/pluggable.php on line 881
Thanks

likely cause?(drupal imagefield conflicts with my module)

Imagefield is not working properly and I get this lengthy error -
{ "status": true, "data": "\x3cdiv id=\"edit-field-image-0-ahah-wrapper\"\x3e\x3cdiv class=\"form-item\" id=\"edit-field-image-0-upload-wrapper\"\x3e\n \x3cdiv class=\"filefield-element clear-block\"\x3e\x3cdiv class=\"widget-preview\"\x3e\x3cdiv class=\"imagefield-preview\"\x3e\x3cimg .............
whenever I upload image. After a bit of detective work, I found out that my own module, which is written for creating custom content type, is causing imagefield to fail. Does anyone know what usually trigger this type of error? Your help is much appreciated.
Imagefield - 6.x-3.7
CCK - 6.x-2.8
Filefield - 6.x-3.7
drupal 6.x
Its tough to say unfortunately. I suspect its something to do with AJAX. Try debugging the Javascript using Firebug in Firefox.
I'm not sure how related this is to your json output but if you have a custom ahah callback sometimes there are conflicts between imagefield or any drupal form file. So it was recommended here:
http://drupal.org/node/399676#comment-1438662
To use drupal_to_js instead of drupal_json when printing the callback status and data.
// don't call drupal_json()
// print drupal_json(array('status' => TRUE, 'data' => $output));
// send the updated file attachments form... .
// ahah.js uses an iframe and the header output by drupal_json() causes
// problems in some browsers.
print drupal_to_js ( array ( 'status' => TRUE, 'data' => $output ) );
exit;
After days of frustration with form fields not keeping thier values until after the imagefield had been uploaded i also found that the ahah.js iframe behaviour of filefield and imagefield can conflict with other ahah functions sitting on other form fields.
Patching ahah.js by following patch #19 here fixed this for me.
http://drupal.org/node/806500#comment-4004316
I had similar problems few days ago. I've used Ubercart module, particularly its product module. There is an imagefield in the product creation form. So I got the same message (JSON output) trying to add picture to product. Important note: such a behavior was observed only in Chrome (I'm on dev-channel of it). Firefox handled form successfully.
I didn't investigate the reasons of such a behavior unfotunately. But I advise you to check your site in different browser(s) too.

Resources