Wordpress user id - php

I have a custom page that submits a form to another custom Wordpress page. I need to check the userID on this page, so I do this :
$user_ID= get_current_user_id();
When i echo that variable I get 0. HOWEVER, if i access the page directly (without submitting the form) it shows the correct userID.
To make it stranger, when i submit the form my admin bar will disappear as if i wasn't logged in, again, if i access the page directly the admin page will appear.
What could be happening when i submit the form that will cause this?
I should mention the page im submitting to is a copy of page.php with a few lines of code to submit the variables to a database. Anybody have some suggestions?

Related

How to go back without asking to confirm resubmission of the form

I have a web page that loads all the data from a mysql database called datalist.php
From this page I can edit record by record with a button that redirects you to an editdata.php page adapted to the fid of the record.
Once edited as they want to see the changes, I don't redirect them to the main one letting them see the changes and simply clicking back or with a button they return to the datalist.php without any problem.
The button is this
echo "<p id='parrafo'><a style='padding:1px 20px'class='button rounded-0 primary-bg text-white w-0 btn_1 boxed-btn' href='javascript:history.back() '><--</a></p>";
PROBLEM
I added a search engine where the displayed data can be filtered.
When they use the search engine from datalist.php, I direct them to a page called search engine.php where, through a post method, I store what they are looking for in a variable and the data that users want appears.
But when they edit a filtered record, it is edited without problems, but when they go back, they return to the search engine.php and the message appears:
"Confirm form resubmission In order to display correctly, this web page needs the data you entered earlier. You can submit that data again, but that will cause the page to repeat all previous actions. Press Reload to submit the data and display the page.
Hit the page refresh button to resubmit the data needed to load the page."
Of course, if they update, they come back when the filtered data comes out.
Isn't there any way to store the variable used in the search so that when I go back I don't get this error or any solution??
simple! when user will submit form for that variable instead of making post request
option1: just use get request __url__?variable=... but this will not remember the variable when you go back
option2: store the variable in the cookie and just go to next page (eg. window.location.href = '...';). and in next page access the cookie from php.
If you are wanting to show the form to the user as a confirmation, but without the possibility of another post, then remove the form element and the button. Display all other boxes as they are (with the values populated from the POST array).
And display another message telling them that it has been successful.
You are using PHP, you can achieve this easily with it. If you are unsure, then post a short version of your code in a separate question.

Wordpress admin page's login form auto-submits

When I go to localhost:PORT/ADMIN.php, I am brought to the login page to sign in as an administrator. As soon as this page loads, it displays:
ERROR: The username field is empty.
ERROR: The password field is empty.
… before I've even clicked the "Log In" button. It also shakes the form. It behaves the same way when I refresh the page.
Is there a way to prevent the form from automatically submitting on first load?
If anyone is experiencing a similar problem, I was able to fix the issue by modifying functions.php. I created a function (that fires on login_init) that checks if there is any post data for the password field. If there isn't, then I created a blank Wordpress error, which has the effect of clearing any other errors.

Showing gated and ungated content based on form submission

I have a HubSpot form embedded into a WordPress page.
What I'm trying to achieve is gated and ungated content. See these use cases as an explanation:
User enters WordPress page for the first time on content that is gated by default (by gated
I mean it shows a form).
User fills out the form and gets redirected to whatever is specified as the redirect
option in HubSpot (the content is now ungated).
User closes the window but decided to back onto the WordPress page. Now, since the
user has already filled out the form (the cookie for hubspotutk
exists), I want the user to be redirected straight to the asset
(whatever is specified in the redirect option in HubSpot forms).
How far have I got?
I've created an ACF field in WordPress with radio buttons for Gated or ungated content. By default, all content is ungated.
I've then set a cookie based on this ACF field value. I.e. if the content is gated, resourceType cookie equals "Gated".
That's how far I have got.
The next steps (I think) would be to...
Only "ungate" the page if the user has filled out that form. For example, I've completed a form, the hubspotutk cookie value is "23a43a4a6de9c38f7657ebd08d574scf". How does HubSpot know which form this value is assigned to?
Other concerns:
If the user has filled out the form already, how to I redirect them straight to the asset? Is there a way to get the "redirect to another page" value in the image below as a variable?
I don't want to use HubSpot Forms API because I don't expect the admin to create the forms via the API. They'll want to create it via the HubSpot forms option.
Any ideas?

How to pass values from lightbox to main page

My main application opens an user page. There are New / Edit / Delete buttons on the same page.
I am using Datatables Editor to display records. When I click on New button an lightbox opens up and the datatables editor build-in plugin shows add user template. When this template is submitted a new record is created. The backend has a AUTO_INCREMENT key which I want to be passed back to main page such that I display a message 'User ID : XXXXX has been created'
The issue is that even though I pass it as a session variable the main page which I believe since it is already loaded and cannot be refreshed after lightbox page is submitted - it never receives the updated session variable. As such session variable though set dynamically when user submits lightbox from backend is never seen in main page
How can I achieve the desired output?

Stay on current page after POST

I have a form which once the Submit button is pressed, it goes to a blank page and returns any error messages on that blank page. However I have a website template and I wish that my script is run, and returns the the page which did the action POST and puts any error messages on that page.
Example of what is happening:
PAGE REQUESTS POST ----> SCRIPT RUNS ---> RETURNS ERROR MESSAGE
What I want it to do is:
PAGE REQUESTS POST ---> SCRIPT RUNS ----> GOES TO THE PAGE WHICH REQUESTED POST ----> SHOWS ANY ERROR MESSAGES WHICH THE SCRIPT PICKED OUT.
Sure you can, just set the Action of your form to the current page. Then the $_POST will contain all the values the user filled in the form.
You have two options. I assume your are going to a blank page, because your POST goes to a PHP script different from the page it is posting from.
Option 1 - Post to the same PHP page as the page your are posting from.
Option 2 - Once you have posted, and generate an error, redirect back to the page, with the errors stored in a session, or a cookie, so that they can be rendered on the original page.
Assuming that the page containing the request form is also able to display the error messages, set the form attribute to the url of the page containing the form (as TJHeuvel noted) or just make action attribute empty, e.g, <form action="">.
After going to blank page store all the posted variables in your variable say $get with syntax param1=value1&param2=value2 and so on...and redirect back by header("Location: back.php?$get"); and in your main page by $_GET echo all the vars

Categories