I have a front-end editing form, where users can update specific post data (ACF custom fields). The problem is, that the users only know the post slug, not the ID.
So I need a page before, where I can convert the slug to post ID, then pass it as a url parameter, and redirect to the update page above.
I know the url_to_postid(site_url("slug")) function, but I don't know how can I pass the slug (from a form or a textbox) to this function, then redirect to a url with the parameter.
Let me know on which of the following steps you want me to explain further.
create a page template in your theme, code below
create a page that uses that template. now you have url for ajax from your front-end.
if it's on same domain you can use ajax fetch. if it's on another domain you can use jsonp which is same idea..
sample code for template file (not tested):
<?php
/* Template Name: My Service */
$slug = $_REQUEST['slug'];
$id = url_to_postid(site_url($slug))
echo $id;
exit();
// or
echo json_encode(['id'=>$id]);
// or
// redirect to url + id
Update - Non ajax solutions
First 2 steps as before. Then make a <form> around that button and <input name="slug"> that submits the to the address of the page you prepared in step 2.
Related
On my Wordpress website I have users fill out a form which ultimately generates a product. One of the form fields is a "Title" for the product. I'm trying to change the redirect for where the user is sent after they submit the form.
The URL for products are based on the "Title" of the product (e.g. http://example.com/product/My-Great-Product-Title/)
The issue with my current code is that I get a returned URL of... http://example.com/product/My%Great%Product%Title/
This returns a page not found 404 error.
Naturally a product title with no spaces in between the words works just fine but for obvious reasons every title can't be limited to one word.
My current code for redirecting which results with the 404 error...
$redirect = "http://example.com/product/$title";
How can I get my URL to be "My-Great-Product-Title" instead of "My%Great%Product%Title" which doesn't seem to work.
Assuming that the URL is generated by WordPress whenever the Product is created and now created by some other method you should be able to use sanitize_title() which is what WordPress uses to create their friendly permalinks.
The drawback to this is if there's a duplicate, it won't detect this but WordPress will append a -2 to the url. A better solution would be to get the ID after the product has been created and use get_permalink() which will be more accurate.
I have a custom Wordpress plugin that handles many things including download pages for my products. The links are not the path to the actual PHP page to my plugin. For example...
http://myurl.com/download/product
But the path to my plugin that actually handles the download function is this path:
http://myurl.com/wp-content/plugins/myplugin/myplugin.php
I have built an app that uses these download links to grant a user the ability to download a product after purchase. But now I need to send extra data along with the url. How can I send some additional $_POST data when the users click on the download link. I have tried
http://myurl.com/download/product?id=2345&user=tom
But when I try to echo out the variables in myplugin.php I get nothing.
echo $_GET['id'];
echo $_GET['user'];
I have also tried to use a form and send hidden inputs but still the same result.
Any help would be greatly appreciated.
Create a new shortcode (maybe empty that does nothing) and put it on some page you created from WordPress.
Then, form action will be the URL to new page where you send data you need to send and method will be post.
You can handle the data you sent in new shortcode function in your plugin using $_POST['data']
This might help you as an alternative solution.
I'm using wordpress with a custom crm for a 2 part form.
When I send the first form data through from the form on the homepage it returns a customer ID in the URL to the second form page. However, the &recordid=xx makes wordpress returna 404 page.
How do I make the page not through a 404?
Original form second page: http://mysites.com/get-personalized-quote
Original form second page with variable: http://mysite.com/get-personalized-quote&recordid=2763
You should use ?recordid=2763 instead of &recordid=2763
note the ?
I built a custom component for Joomla 1.5. It' an FAQ component.
I'd like to let users add questions from the Front-End.
I have several fields that shouldn't be displayed for the user on the front end.
For ex. in the back-end admin has fields like Approved, Ordering and Published and the rest. I would like to let any user without signing in to add question front the front-end but these 3 fields shouldn't be displayed to the users on the front-end.
So, how to build the front-end user input?
Maybe someone has done that or know some good tutorial for this case?
In the view.html.php file of your component (eg. com_faq/views/view.html.php) you can define the mark up for your input field section. I build up a $html variable like:
$html .= '<input name="addQuestion" value="" type="Text"/>';
then add a reference to it:
$this->assignRef("addQuestion", $html);
so that in your view template (i.e. com_faq/views/tmpl/default.php) you can add it to your page like
echo $this->addQuestion;
When you click your submit button you can reroute back to the same view. So user a url like
index.php?option=com_faq&task=addQuestion&view=default
So before you mark up your page (so within the first few lines of your display function for example) you can grab the contents of your user's input on the front end
$question = JRequest::getVar('addRequest', null);
Once you have this you can either store it to your database or display it. Alternatively you can AJAX submit your form and process it in a controller function so the you don't have the refresh etc.
You will need to edit your router.php file to pick up the task and pass it to your controller i.e. set it as a task or a view.
There are loads of options for this but fundamentally there are 3 things you need:
Create your mark up in your view.html.php file and assign a reference to it
Include the reference in your template i.e. default.php
Submit your form to an address that your same component can process it i.e. index.php?option=com_faq&task=addQuestion&view=default
Hope this helps :)
Each user, except Admin, will only have one post each, a custom post type - which is a questionnaire fill-out. I already have the login redirecting to the custom post type page, but I'd like to go one step further and make it really easy for the user.
If they are logging in for the first time, it would continue to redirect them to the custom post type page, but also auto click on the add custom post type button.
If they have already begun the questionnaire, it would auto click the edit button.
I have an auto click working elsewhere, but it's being triggered by a live click on another button.
jQuery('.steps-button').live('click', function() {
jQuery("#publish").click();
});
I've tried using jQuery(".row-actions").find(".edit").click(); but it's not working.
update, from functions.php
add_filter('login_redirect', 'plugin_admin_redirect');
function plugin_admin_redirect($redirect_to, $url_redirect_to = '', $user = null) {
return 'http://mywebsite.com/wp-admin/edit.php?post_type=nanny_profile';
}
earlier in functions.php I have under jQuery(document).ready
jQuery(".row-actions").find(".edit").trigger('click');
Am I putting it in the wrong place, perhaps?
You can try trigger:
jQuery(".row-actions").find(".edit").trigger('click');
Here is a jsFiddle example how to use trigger.
.trigger()
I think you should not place the code:
jQuery(document).ready
jQuery(".row-actions").find(".edit").trigger('click');
in function.php, function.php runs in your server, your questionnaire runs in your user broswer. they never work together. place the jQuery code in the custom post type page template instead.