5

I'v built a custom widget that will show some sport scores. Scores are added trough custom post. When i open new custom post(scores) i enter their team names, score, scores. Now i want to add an option to show team logos next to the team name. In the last couple of days every tutorial here and elsewhere i could find didnt worked. I have no idea why, as i am newbie with all this. With many of it, upload button appears but the pop up to chose/upload image doesnt appear. Now I could post at least 10 links where i found this codes but i believe there is a limit with link. So for an example this is most common tutorial i have found.

Here

The last one. Now in this case i am not sure about few things.

add_action('admin_enqueue_scripts', 'my_admin_scripts');

function my_admin_scripts() {
    if (isset($_GET['page']) && $_GET['page'] == 'my_plugin_page') {
        wp_enqueue_media();
        wp_register_script('my-admin-js', WP_PLUGIN_URL.'/my-plugin/my-admin.js', array('jquery'));
        wp_enqueue_script('my-admin-js');
    }
}

what exactly is this 'my_plugin_page' i guess i am doing something wrong and that's why uploader doesnt pop up. What i need to add instead of it, any example?

Inside my theme i'v made a new folder called my-plugin inside which i added my-admin.js with the script code from the link above.

I also tried other way without it can calling the script by get_template_directory_uri().

Any help pls.

1 Answer 1

0

Firstly, it is not recommended to use WP_PLUGIN_URL constant, instead use plugins_url() function.

Second thing, it will not work when you put your files in the theme. You must put the necessary folder and file in the plugin if have to use this constant or function.

Make sure, you have put correct path and you JS file is being loaded properly.

wp_enque_media() is used to load necessary files and settings needed for a media uploader in the WP admin area. However, you should add necessary code specific to your uploader i.e. code for uploader on your custom post page.

The link that you have shown has covered enough code to implement WP media uploader in a plugin/theme.

Checking for my-plugin-page is not necessary. It's added just to make sure that your script loaded only on necessary page and not all the pages. That too will be applicable when you have added an admin menu. In your case, I believe, you want the uploader on your custom post type edit page, so you can check for that instead. Or for time being, just remove that condition, add necessary code in your my-admin.js and html in one of your plugin's file as per tutorial given in the link shared by you.

4
  • When you mention plugin, i thought perhaps it needs to be a plugin in order to work. As so far i had one part of the code in function.php (one to creat custom post) and other part as widget (score.php). I'v spent 5 hours trying and learnings how to build a plugin/widget from these two things and finally I managed. This is it pastebin.com/7mC6QpHQ But still it doesnt work and i dont know why (newbie, remember). Note the line 176-185 which i added. Script i tried to placed both in my-plugin folder, my theme js, custom js and no luck. I tried all methods but pop up just wont work.
    – Max
    Commented Aug 17, 2015 at 4:51
  • Also i tried to add function my_admin_scripts() both in function.php and inside plugin (which i added to pastebin), as i more try and error guy but nothing..What i am doing wrong or is this even a way to do it, i have no idea :D
    – Max
    Commented Aug 17, 2015 at 4:58
  • Ok, I managed to get it working, but now the issue is, it doesnt save it. So what now?
    – Max
    Commented Aug 18, 2015 at 5:06
  • Update: I have these two codes pastebin.com/78djwMtp The first part is an array to create a box inside custom posts area and the below part is to upload an image inside that box thing is, it uploads in the first box ok, but on the second it uploads also on the first. I tried changing the below part but I'm not an expert and it could be that the issue is also with the id's (top part as they use the same upload, considering there's java included. I v also tried to echo below part once more and then just change image1 to image2 but no luck. Can someone tell me how to work this out?
    – Max
    Commented Aug 20, 2015 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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