assign post ID to new wp.media object - wordpress

I have a custom form for publishing posts. When the new post is first created, there is no existing post ID, so attaching photos to the post doesn't work until after the post is saved and an ID is generated for the post.
The first thing I've done is initiate an auto-save so that a post ID is generated to represent the new post. However, I'm can't figure out how to update the wp.media object with this new ID so that uploaded posts are assigned to the ID generated during the auto-save.
Is there a way to reinitialize the wp.media object with the new post ID?
edit
I found this resource, but it doesn't seem to cover how to assign an post ID, and I can't seem to locate it in the source code referenced by the git either...
https://github.com/thomasgriffin/New-Media-Image-Uploader/blob/master/js/media.js
update
Looks like I was able to reassign the post ID to the wp.media object by simply doing this:
wp.media.model.settings.post.id = new_id;
However, I'm experiencing some permission issues now. As an admin, everything works fine, but as any other user, I get an error "An error occurred in the upload. Please try again later."
Uploading works fine with all users once the post is saved, but for some reason it won't allow me to upload with non-admin users after assigning a new id to the wp.media object.
R

Finally sorted it out.
AJAX is used to initiate the auto-save in the background while the user edits the post. When the auto-save ID is returned, it's re-assigned to the wp.media object like this:
wp.media.model.settings.post.id = autos_save_id;
The permission issue I was experiencing came from the simple fact that my user role didn't have the 'edit_post' capability. Duh.

If I remember correct the media has an ID just like post/pages. It is up to the editor/system to associate media to post.
When I upload an image (media) to my local dev I edit it via :
site.com/wp-admin/post.php?post=19&action=edit
Where 19 is the media ID.
if I go to site.com/?p=19 I am forwarded to site.com/?attachment_id=19 and get to see the media.
Regarding the post/page (custom form) - when implemented correctly, on first save (submit) the post gets an ID. That ID remains the same. (beware of autosave post IDs and similar).
Tell me more about what you know regarding the "wp.media object" or let me know if I missed something?

Related

Modify custom field from front end

If I need to manage a custom field under wordpress, but not from the admin area. In other words, I need that users modify the content of post custom fields from front end. What is the way to do so?
As instances, please to imagine Stackoverflow.com as a wordpress site. Under this site, the number of upvotes/downvotes and the accept state are my 2 post custom fields. How can I make the front end user modify their values in WP database?
Please If you have another better approach to create votes system and accept system, using wordpress, it will be highly appreciated. But if my approach (using post custom field) is okey, please to guide me find documentation from web or codex to achieve my goal.
Thank you for your usual help.
I would suggest thinking about a couple things:
Each post should show the sum of up-votes and down-votes
Each user should only be able to up or down-vote once (i.e., one user can't vote the same post up a hundred times)
You think you would want to create a function for your theme that allowed users when logged in to alter the metadata of a post. However, this isn't really the case – what you need to do is to allow users to edit an element of their own metadata (see add_user_meta) that is basically just a hash. The metadata could be like array("post-666-vote" => -1, "post-777-vote" => 1) for a downvote of post 666 and upvote of post 777.
So, when loading each post, your vote-tally-renderer would look something like this:
vote = get_post_meta($user_id, "post-" . $post_id . "-vote", true);
if(vote == -1) {
// Render down-arrow voting
} elsif(vote == 1) {
// Render up-arrow voting
} else {
// Render normal arrows
}
Each arrow would probably have to make an AJAX request to update the post's metadata. This is a long tutorial (but it's not super complicated! I promise!) and about 3/4 of the way down it talks about how to use an AJAX request to modify some user's metadata. http://wp.tutsplus.com/tutorials/plugins/a-primer-on-ajax-in-the-wordpress-frontend-actually-doing-it/
The only real major change is that the PHP function that is called by the WP AJAX request would also need a callback function to update the post's metadata, and change the number of votes on the post itself. This way, when the page is reloaded, it will show the proper number of votes.

Wordpress custom fields not saved in database

I've been running a WordPress site for over a year now and never had a problem with it until yesterday. I'm using a custom field to store the url to a preview image for every post. I simply add it using the standard Custom Fields form in the Edit Post screen. Before when I'd save the post as a draft or if I immediately published it the custom field would be saved. But now all of a sudden no matter how I save the post (save as draft, publish or auto-save) the custom field isn't being stored in the database. I did confirm this by checking the wp_postmeta table.
I already reinstalled Wordpress, but it doesn't change a thing. I also went through other similar posts about this subject, but none of the solutions work.
Any ideas?
So after digging deeper, I figured out the custom field was dependent on another field. Still not sure why it wouldn't store the custom field in the database even though the other field wasn't filled out first. But it works anyway when I do it that way. WordPress works in mysterious ways sometimes.

Adding an sql query to the end of a wordpress permalink?

Ok....so I created a new wordpress theme and I still can't get my custom code to work.
I want to be able to have a page which will output a list of items from the database, each item will have this URL format:
www.site.com/category/subcategory/?id=XX
The ?id=XX query should be at the end of the URL and will direct the user from the category page to the subcategory page showing specific data about the item with the XX id.
Each item in the list is wrapped in an tag in the category template page and the subcategory page uses $_GET. I've got one wordpress theme that works but on my other site, I use a different theme and it doesn't work. If a user clicks on an item in the list, the user gets directed to the subcategory page without the sql string attached. I'm not sure what to put in the functions.php file in wordpress (or whatever the file is called that handles functions) or in the htaccess. Any help is appreciated.
Update:
#Niels - I didn't make the code...I had two people make the code for me. Neither of them are professional coders but they got it to work so that's all I cared about. My website has never been hacked before but I need to find out how to get the code to work myself since the two guys who created the code are busy nowadays. Yea, I've been told about the sql injection.
I've got php code on the category page (http://www.mysite.com/category) that defines variables that will be used in the code, connects to the database, queries the database table, and then selects and outputs the items in the database table using the id of the item. The code uses a while loop to output the items so that the category page is basically a directory of items. Each item that gets outputted is outputted in this format: $itemname. Like I said, the code already defines these variables at the very beginning so the sql query already knows what to look for. When a user clicks on any item in the category page, the link should take them to the item's profile page, which is basically the subcategory page (ie: http://www.mysite.com/category/subcategory/). So let me be clear: when a user CLICKS on an ITEM in the CATEGORY PAGE, the user should be directed to the item's profile page, which is basically the SUBCATEGORY PAGE. This is where the /?id=$id part comes in. During the transition phase from category page to subcategory page, the query string should attach itself to the URL and the subcategory page will output data based on the data that is in the database for that specific item. For instance, if a user clicked on a link such as this: www.mysite.com/category/subcategory/?id=12, the user should be directed to the profile page of the item with the id of 12. The profile page, which is the subcategory page in our case, should output data for the item in the database that has the id of 12. That's how it should work. The subcategory page, or item profile page, will have a $_GET code so that it knows what data it is suppose to output.
Revised Answer
Look into WordPress' Rewrite API. It's definitely the more "correct" way of doing this.
Original Answer
Instead of using a URL query, try something like:
// URL: http://blahblahblah.com/category/pagetitle/15
preg_match_all('/[^\/]+/', $_SERVER['REQUEST_URI'], $matches);
$item_id = $matches[0][2];
echo 'Do whatever you want with Item ID ' . $item_id;
If a user clicks on an item in the list, the user gets directed to the
subcategory page without the sql string attached.
What do you mean exactly with "sql string"?
Could you describe, as concrete as possible, what links you have, examples and what page they should direct to and what those pages should do?
Also: I get the feeling you're a little out of your depth here and you're doing stuff with the database based on parameters from the URL . You should know that manipulating URL parameters to gain access to a database is a real easy way to hack a site. Please read a little bit about SQL Injection and how to prevent it.

drupal 6 VIEWS - I'm stumped

I've created a view (Drupal 6) that gets put on the users profile page.
The view is supposed to list all the images that the user has uploaded. However, the view is showing every uploaded image by all users.
(When a user uploads an image, it first gets approved by me, and then posted by me and displayed on the homepage with a credit to them and a link to their user profile -- so they are not the "author" of the post, I always am.)
How can I limit the photos displaying on their user profile to just show the images uploaded by that particular user?
Thank you.
Use arguments handling: add argument - User ID, check "if argument not provided" and select php code: return arg(1); // if views shown as block and you want to show it in user profile, path will like user/UID, so arg(1) return UID.
2Ted: hello Ted! php code will absolutely secured, if you will check it, for example via is_numeric() function. Also users cann't get any images, becuase Views wil use Block display, and you cann't provide for this arguments directly.
2user967969: Additionally about approving: You can create second field, where you will store user profile UID. So for my above text you should use this field for argument handling, instead of user uid.

How to modify the Node Edit or Insert Form in Drupal

I am having trouble finding sources of information or example code for a creating a custom module (or any means) to edit the node edit/insert pages.
I am trying to create a Flickr Integration for a node. The Flickr API is not an issue and i can resolve those, it's the Drupal API issues i could use some help or resources of information on.
Here is what i am trying to achive.
User attempts to add or edit a node
User inserts a keyword into a field and presses a button (Get Photos)
Flickr API returns and displays a few images
User clicks on an image and the URL of the image is then added to an input field
on node save or node update a field such as $node->flickrImage[0][value] is updated with the URL selected in 4.
the variable is the available when ever the node is rendered.
I'm not quite sure how to achieve this - I simply need some example code of modifying the node edit/insert pages and I think I can work the rest out.
Please Help!
Thanks,
Shadi
It looks for me, that you can write own CCK field type, so that you can add this to desired content type and process user's input & work with flickr API.
this way, it's easier to manage this field and control it, plus it will be automatically added to node edit/create forms, node loads, etc.
This article might help http://www.lullabot.com/articles/creating-custom-cck-fields
Second way, is to use hook_form_alter
function module_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'product_node_form') {
//do smth
}
}
In this case, form_id you want to change, will be {content_type}_node_form.
there you can add your field, and process it on
hook_nodeapi
Here is a link for node edit form alter solution ..
http://drupal.org/node/101092

Resources