I want to modify TinyMCE in WordPress so that it inserts media (image etc.) always at the beginning of post contents. For this I need to change the cursor position to the beginning, insert media and possibly restore old cursor position. I am not sure how to achieve it. Please advise.
If your goal is to get a photo at the top of every post, I'd encourage you to use the featured image instead. It can be added to any post type with
Then the user can upload an image to the "Featured Image" field and you can display that in your template immediately preceding your content:
If you need more than one image, add a custom meta box or look into one of the plugins that comes with an image meta box like Advanced Custom Fields or Types. You can then allow for more images to be uploaded and templated however you like.
A final option would be to have people upload photos on a page so that they're attached, and then stick a gallery immediately before
I found this which helped me moving caret to start of post before inserting media. My modified code is: