jquery - Dynamically update post title in admin page

I have a textarea in admin post page when creating new post, that's title:<textarea id="post-title-0"

I have a textarea in admin post page when creating new post, that's title:

<textarea id="post-title-0" class="editor-post-title__input" placeholder="Add title" rows="1" style="overflow: hidden; overflow-wrap: break-word; resize: none; height: 94px;">vbhgfhfgh</textarea>

I want to update title on the fly using jQuery. I tried

$('#post-title-0').val('some value');
$('#post-title-0').text('some value');

I also tried using plain javascript.

val() updates title but when I click on title it is being erased. Is there anyway to update title via JS?

I have a textarea in admin post page when creating new post, that's title:

<textarea id="post-title-0" class="editor-post-title__input" placeholder="Add title" rows="1" style="overflow: hidden; overflow-wrap: break-word; resize: none; height: 94px;">vbhgfhfgh</textarea>

I want to update title on the fly using jQuery. I tried

$('#post-title-0').val('some value');
$('#post-title-0').text('some value');

I also tried using plain javascript.

val() updates title but when I click on title it is being erased. Is there anyway to update title via JS?

Share Improve this question asked Jun 12, 2019 at 6:44 FosAvanceFosAvance 1435 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 8

The block editor is restoring back the title that was last saved (or typed manually into the textarea), so with the block editor, you can change the title dynamically using this code:

wp.data.dispatch( 'core/editor' ).editPost( { title: 'Title here' } )

PS: You should make sure your JS file has the wp-editor, wp-edit-post or wp-data as part of the dependencies.

UPDATE

Here are the resources which helped me identify the above solution/code:

  • https://developer.wordpress/block-editor/packages/packages-data/#dispatch

  • https://riad.blog/2018/06/07/efficient-client-data-management-for-wordpress-plugins

  • https://github/WordPress/gutenberg/blob/master/packages/editor/src/components/post-title/index.js (the React component which setups the title textarea / UI)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745407511a4626379.html

相关推荐

  • jquery - Dynamically update post title in admin page

    I have a textarea in admin post page when creating new post, that's title:<textarea id="post-title-0"

    5小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信