• How can I set the default value (so always the same) of post_content for all posts of the same custom type?

    post_type=property
    post_content=”default content”

    So basically I need when the user submits a new posts of type property, he cannot edit the post_content himself (so the editor is disabled), and the system automatically sets its value to “default content”? As I understand I need to add code to the functions.php file?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Few questions:

    1) Is the user submitting a post under property post type from backend or front end?
    2) What is the role of a user submitting content?
    3) What is the role of a user who will be reviewing the submitted post, updating the actual post content and then publishing it?

    If you are accepting content and want to review it before publishing, then instead of disabling post content editor and setting default content, I suggest assign “Contributor” role to all those users. Then submitted posts will be held for review. The ‘Editor’ team would then review and publish the final property post.

    Let us know what you think.

    Thread Starter Janis Ozolins

    (@janisozolins)

    1) From the admin panel.
    2) Admin.
    3) The content won’t be reviewed, updated or edited. I need it always to be the same (consists of shortcodes) for this particular post type “property”. The user will use the custom fields and these fields will populate the post_content with the help of shortcodes. I just want to prevent user from ruining the shortcode order and the layout of that post as it needs to be the same always.

    I hope you get what I mean.

    This cannot be done with user roles this time, I am certain about that. I need a PHP function that will store default content (you can think of it as a template) into the database once a new post of type “property” is submitted by the user.

    Unfortunately there is no such property using which we can show content editor in disabled state. You can either completely remove it or have it there. If you disable it using remove_post_type_support then the default content does not get saved.

    So I have following workaround for you. Place this code in your theme’s functions.php file:

    /* Here you set default property content using default_content filter. */
    function set_default_property_content( $content, $post ) {
    
    	if ( $_GET['post_type'] === 'property' )
    		$content = "This is default property post content";
    
    	return $content;
    }
    add_filter( 'default_content', 'set_default_property_content' );
    
    /* Here you include a js file containing a code to disable editor. */
    function disable_property_editor() {
    
    	$screen = get_current_screen();
    
    	if ( $screen->post_type === 'property' ) {
    
    		wp_enqueue_script( 'editor_disabler', get_template_directory_uri() . '/js/editor-disabler.js' );
    	}
    }
    add_action( 'admin_head', 'disable_property_editor' );

    Finally here is the code which shall go in /js/editor-disabler.js file in your theme folder. The js code here does not actually disable the editor but puts an overlay container on the WordPress editor.

    jQuery( document ).ready( function() {
    	jQuery("#postdivrich").wrap("<div class='disabled-editor'></div>");
    	jQuery(".disabled-editor").css("position", "relative");
    	jQuery(".disabled-editor").css("z-index", "-1");
    	jQuery(".disabled-editor").css("opacity", "0.4");
    });

    Once you have all above code in place then clear the browser cache and check the result. I hope this helps.

    Hi Janis,

    Let me know if above helped else I may assist you further.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to disable the editor but still set the post content?’ is closed to new replies.