Wordpress wp_editor won't add image - wordpress

I have the following code inside a plugin in a loop, so there are multiple WYSIWYG editors:
<?php wp_editor( stripslashes($arr['item-content']), $key.'-item-content', array(
'editor_class' => 'tsort-contarea',
'media_buttons' => true,
'editor_height' => 360,
) ); ?>
When adding an image, the XHR request labelled send-attachment-to-editor inside wp-includes/js/media-editor.js has a wp.media.view.settings.post.id of 0. Also, wp.media.view.settings.nonce.sendToEditor is always this value: e8b2eea867
return wp.media.post( 'send-attachment-to-editor', {
nonce: wp.media.view.settings.nonce.sendToEditor,
attachment: options,
html: html,
post_id: wp.media.view.settings.post.id
});
The xhr request fires off fine, but doesn't add to any of the WYSIWYG. I'm sure that's because the post_id isn't set or because the nonce is not unique. What can I do to make this work? The Media Manager works absolutely fine on content pages.
Source file: http://pastebin.com/BhvqBLGB

From Codex:
Note that the ID that is passed to the wp_editor() function can only
be composed of lower-case letters. No underscores, no hyphens.
Anything else will cause the WYSIWYG editor to malfunction. (As of
3.6.1 you can use underscores in the ID.)
From what I see you are currently using a dash. Try changing that, and see how that works.

You should check thoroughly for the wp.media.view.settings.post.id is 0 part, as I tried to reproduce the bug in metabox with multiple editors and I get post-id for the new post equal to non-zero (which is actually an auto-draft record in wp_posts table), image assignment is working properly for this case.
I believe there is something wrong with saving new post as auto-draft in your instance of WordPress (might be because of some plugin/theme)

Related

WordPress Gutenberg, update post content programmatically

I have been starting to testing out Gutenberg editor with both ACF and custom blocks. And I have been looking around to solve my problem but I couldn't find anything about this topic (Maybe my google skills is not good enought)
But my case is this:
I have a custom post type where I want to set a template so they can't move around the blocks and add other blocks and so on. And on this post type around 70% is created by code. Because it is fetching all the information from an API.
Test 1: I have created an ACF block with all the fields I need, and it is working as it should when I create a new post from WP admin. But when I run the update_field function it is saving it to post_meta table as it did before. So my question here is how do I update a field so it saves it to post_content and not to post_meta table.
Test 2: I created custom blocks for all of the fields (convert each ACF field to and block) and set up the template to use these blocks only.
But here I have no idea how update update post_content with PHP or Javascript.
I hope you can help me out with this :) If anything is unclear tell, and I will try to explain it
ACF has an ability to pre-init fields before post will be visible to user on post creation page. You can try to use this function to set desired content to fields.
You can read about this here:
https://www.advancedcustomfields.com/resources/acf-prepare_field/
As of Wordpress 5.0.0
You can use template and template_lock arguments upon registering your custom post type.
Source # https://developer.wordpress.org/reference/functions/register_post_type/#changelog
You can then set an array of specific blocks to use and you can chose to restrict users from adding new blocks or removing them.
Attribute
Description
template
(array) Array of blocks to use as the default initial state for an editor session. Each item should be an array containing block name and optional attributes.
template_lock
(string/false) Whether the block template should be locked if $template is set. If set to 'all', the user is unable to insert new blocks, move existing blocks and delete blocks. If set to 'insert', the user is able to move existing blocks but is unable to insert new blocks and delete blocks. Default false.
A short example would be something along the lines of...
<?php
$args = [
//...
'template_lock' => 'all',
'template' => [
[ 'core/paragraph' ],
[ 'core/file' ],
//...
],
//...
];
register_post_type( $post_type, $args );
?>
Currently, Gutenberg documentation is scarced, you can find a complete list of blocks & parameters # https://github.com/WordPress/gutenberg/tree/master/packages/block-library/src
Gutenberg is still in development, some features don't act as they should.

wrap visual composer element(s) in a shortcode

I have some content created with visual composer and I want to wrap some of it in a shortcode like
visual composer elements
[is_mobile]visual composer elements wrapped in shortcode[/is_mobile]
other visual composer elements
please how can it be done? thanks
I believe you need to "register" your shortcodes with Visual Composer. Having unregistered shortcodes confuses Visual Composer; it doesn't really know what to do with them, so it ignores them or does weird things with them (in my case, the content I was trying to wrap in an unregistered shortcode ended up at the top of the page).
So in the following:
name shows in the grid of Visual Composer elements when you are editing a page. You also use it to add your shortcode to the VC Container class (WPBakeryShortCodesContainer).
base is your shortcode's name -- in your example that is is_mobile
as_parent indicates which shortcodes your container can accept as children. I have it set to "except" nothing -- meaning it accepts all Visual Composer shortcodes as children. You can also set it to "only" and list out specific shortcodes you'd like to allow as children (for example if you only wanted to allow people to show or hide an image gallery).
And stuff that didn't matter for me: I'm not really sure why, but I was able to use is_container as true OR false. It made no difference in my situation. show_settings_on_create and content_element are probably irrelevant to your purposes but if you want to know more, they are explained here on VC documentation for vc_map.
This registers your shortcode with Visual Composer:
vc_map( array(
"name" => __("Is Mobile", "my-text-domain"),
"base" => "is_mobile", // your shortcode name
"as_parent" => array('except' => ''), // Use only|except attributes to limit child shortcodes (separate multiple values with comma)
"content_element" => true,
"show_settings_on_create" => false,
"is_container" => true,
"params" => array(
// you can add params same as with any other content element
// i didn't have any options to add onto my element; i was just trying
// to show or hide content based on WP conditions irrelevant to VC
),
"js_view" => 'VcColumnView'
) );
And this makes your shortcode act as a container (i.e., accept other VC elements as children) by extending the default VC container shortcode class. It seems to use the name from the above snippet as the connection.
if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
class WPBakeryShortCode_Is_Mobile extends WPBakeryShortCodesContainer {
}
}
This page on VC documentation helped me figure this out, though it's fairly sparse.

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/

Create a blog post from content in another post type

The Wordpress site I'm working on has a section for "News" (which is the regular blog/posts) which will be used for any news the company has to write about. Then I have a custom post type for Promotions, which has it's own page.
I want the client to be able to add his promotion content through the custom post type, which is going on the Promotions page, however I'd like this content to also be "cross posted" into the blog/news without forcing the client to write it up twice.
Is there a way to do this? Thanks.
Just a note: The reason I have the promotions as a custom type on it's own instead of just having them do it all from the blog is because I needed custom fields that would be unnecessary for any other kind of blog post.
Two options:
1) Use the Shortcode API
And in your cross-post you'd add the shortcode [crosspost id="POST-ID"]. Where POST-ID corresponds to the numeric ID of the other post (post type). Instead of ID, the title could be used, see the function get_page_by_title.
Create your own plugin for this. Add a sample shortcode from the Codex and use the function get_post to get the contents of the cross-post.
2) Use Advanced Custom Fields plugin
With it, adding meta boxes with custom fields is a breeze. And it has a Post Object field that's basically a Cross Post functionality.
You could do it a lot more simply by adding a filter to wp_insert_data(). For example, in your theme's functions.php file add the following:
add_filter('wp_insert_post_data', 'post_to_other', 99, 2);
That filter will then run anytime you add a new post. In the function post_to_other(), you look to see what type of post is being submitted. If it's a promotion, then insert a second copy as a News item.
function post_to_other($post_id, $post){
/** check $post to see what type it is, if it's a promotion */
if($post->post_type == 'promotion'){
$second_post = array(
'post_type'=> 'post',
'post_title'=> $post->post_title,
'post_name' =>$post->post_name,
'post_content'=> $post->post_content,
'post_author'=> $post->post_author,
'post_status'=> 'publish',
'tax_input'=> array('taxonomy_name'=>array('news'))
);
wp_insert_post($second_post);
}
}
I'm running out the door so I don't have time to double check the exact code but that's the basic structure of it. The tax_input bit is optional, lets you specify a category if you want. You'll probably need to tweak it a bit but that's the basics.

wp auto insert tag in posts

when I edit a post in wp sometimes I don't assign a tag , is it a good Idea to auto insert a tag like the category name or my site name instead of no tag at all?
and if its better to input a tag then I need help writing a function that check if the post has no tag then it will insert lets say my sitename.
all what I have is
wp_set_post_tags( 42, 'mepanorama', true );
but this will work only on specific post and does not check if there is a tag already.
This will return the number of tags:
count(wp_get_post_terms($post_id, 'post_tag', array("fields" => "names")));
I made a plugin that adds tags to posts automatically, it is called Automatic Post Tagger. Check it out, it might be useful for you. :)

Resources