0

I need to write some function so that WordPress can get uploaded image from remote client tool, and found this one seems to fit my need: How do I set a featured image (thumbnail) by image URL when using wp_insert_post()? However, I don't know how to add-in code snippet suggested in answers. I mean the code will use some functions of Wordpress, then I don't know where I should put the code, which files I should include...

Sorry if my question is not clearly explained somehow.

1

You can create a simple plugin file to add in the code that you want. In your WordPress folder there is a wp-content directory, and a plugins directory inside that. Create a php file inside the plugins directory, and name it something unique and descriptive; use dashes - to separate words in the name:

ben-is-neat.php

Inside the plugin file, create a comment block with some info about the plugin:

<?php
/*
Plugin Name: Ben Is Neat Plugin
Description: Ben does neat things.
Version: 0.0.1
Author: Ben
*/

There are other plugin headers that you can add to this comment block, but the "Plugin Name" is the main one; without it, WordPress will not recognize your file as a plugin.

Add the rest of your code below that comment block. If your plugin gets complex, you can move your file into it's own folder, just name the folder the same as the php file, and add other files in the same folder. Use the php include() function to share logic between files.

Enable your plugin in the WordPress admin plugin screen.

  • Thanks so much for your detailed explanation. I understand how to add a function in now. Then how can I use it? Like, for my case: I'll upload image to server, then how to trigger that new function to add image into media library, retrieve info of uploaded image like ID, url at client tool... My whole story is to write client software that posts articles to WP remotely. Posting text articles can be done via xmlrpc api. However, with image in post and setting featured image for it, I have no clue of how to do. Thanks again – Ben Nov 17 '16 at 6:47
  • 1
    That's a different question, at least the way I read your question above. Can you try this, get started, put the code in that you're attempting to use, and see what happens? If it doesn't work, then post that code, show what you're having trouble with, any error messages, and information about research that you've tried? – guiniveretoo Nov 17 '16 at 6:49
  • I see, but at least I need to know how to call the added-in code – Ben Nov 17 '16 at 6:51
  • 1
    You need to research and learn about it too; Google is your friend, I'm not going to do your homework for you after I just wrote a novellette in an answer. – guiniveretoo Nov 17 '16 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.