ianharris02
Forum Replies Created
-
Hello,
I deactivated my plugins to see if they conflicted with the Profile Photo plugin, but it threw the same errors each time. Have you encountered this issue before? Right now, I have the main UM plugin installed as well as the Profile Photo plugin, and that is all I have done regarding UM.
What is odd is if I were to edit the CSS for the photo so it is in the center of the page, the upload button does not seem to register as a button, but rather a div element. Is this intentional? I also noticed the camera icon and text from the tutorials are not loading either. Here is an image of this.
What would you recommend I try next? Thank you!
Hello,
Thank you for the quick response. I deleted the code in my functions.php file and downloaded the zip file for the Profile Photo in the Registration Form, but I am still having some trouble getting it to work. The plugin is installed and activated and I get the same errors in the image here.
Do you know why this may be? Is there perhaps a function I need to include somewhere to activate something?
Thank you for your time,
Ian
Hi, I do have “wp_head” which is why it is odd it is not showing up. On a side note, I changed “admin_enqueue_scripts” to “wp_enqueue_scripts” and the error went away. I also added “add_action(‘wp_ajax_nopriv_lesson_watched’, ‘add_lesson’);” as well. I am however having some trouble getting my PHP function to run when the ajax script is triggered. If you wouldn’t mind taking a look at it, that would be excellent!
Currently what I have is this.
function add_lesson() { $lastValue_php = wp_unslash( $_POST['lesson_value'] ); $current_user_id = get_current_user_id(); $found = get_user_meta( $current_user_id, 'lessons_watched_ids ' ); if( ! isset( $found ) || $found !== true ) : update_user_meta( $current_user_id, 'lessons_watched_ids', $lastValue_php ); else : $lessons_completed = get_user_meta( $current_user_id, 'lessons_watched_ids' ); array_push( $lessons_completed, $lastValue_php ); update_user_meta( $current_user_id, 'lessons_watched_ids', $lessons_completed ); endif; wp_die(); // Ends all AJAX handlers }
The purpose of the function is to update a user’s meta field with all of the lesson ids that they have watched. I am passing through the lesson id string as the ajax variable and then I am getting the current user’s array for ‘lessons_watched_ids.’ If it is empty or is returned as false, I am updating the field ‘lessons_watched_ids’ for that user with the php variable ‘$lastValue_php.’ If the meta field does return an array with values, then I pushed the lesson value to the end of the array and then updated the field. Can you see any potential errors with my function so far?
I tried running it, but I keep receiving an empty array in my user’s meta fields. Thank you for your help!
Hello,
That is correct, the script src reference for my-ajax-script.js shows up, but the my_ajax_obj definition does not show up on an admin screen page’s HTML.
For the enqueue code, I do have that in my functions.php file. The ajax script is in the my-ajax-script.js file, and the call for the ajax script to trigger after a video is watched is in my single-template.php file.
Also, I don’t know if this would affect anything, but I am currently designing this site using a custom theme through WordPress Local.
Thank you for your help,
Ian
Hello,
I do not see a script tag that contains my_ajax_obj. The only thing I found relating to that was the script src reference to the my-ajax-script.js file. Is there something that would cause that script to not appear in the source HTML?
Also, for adding the $deps arg, do you mean add it like this?
add_action( 'admin_enqueue_scripts', 'my_enqueue' ); function my_enqueue() { wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my-ajax-script.js', array('jquery', 'ajax-script') ); wp_localize_script( 'ajax-script', 'my_ajax_obj', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'lesson_nonce_value' ), ) ); }
Thank you for your help,
Ian
Ahh, that was a good catch! I did replace the ‘my_ajax_object’ with ‘my_ajax_obj,’ but I am still getting the ‘my_ajax_obj’ not defined error. I double checked all other variables to make sure they were spelled the same way and I cannot find anything else out of place.
I am a bit confused at this point as to what may be going wrong because I am defining ‘my_ajax_obj’ in the wp_localize_script in the functions.php file so it should be able to be reached in my template.php file.
Can you think of anything that I may still be missing? Thank you again for your help with this; I feel like we are getting close. ??
Hello,
I tried using jQuery(document).ready() around my js code, but I still got the “my_ajax_obj not defined.” I also tried reworking the structure of the code so my js code is in its own file. So when a video is watched, the following js code is called to trigger the ajax script which is in the function ‘lesson_watched’.
lesson_watched();
The function lesson watched then calls the ajax script.
function lesson_watched() { console.log('Lesson watched fn called.') $.post(my_ajax_obj.ajax_url, { _ajax_nonce: my_ajax_obj.nonce, // nonce action: "lesson_watched", // action call lastValue_php: lastValue // data }); }
In my functions.php file, I tried using a different enqueue format for the enqueueing and I changed the ajax script location to ‘/js/ajax-script.js.’
function my_enqueue() { wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/ajax-script.js', array('jquery') ); wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); } add_action( 'wp_enqueue_scripts', 'my_enqueue' );
Also, when I tried using the wp_add_inline_script() function instead of using wp_localize_script(), I got an immediate error in the console saying “my_ajax_obj is not defined” rather than after a video is watched and the ajax script is triggered.
Thank you for your continued help,
Ian
Hello,
I am confused. Why would the code not work when combined together? Don’t I need to enqeue the ajax script and use localize to create the ajax object?
I noticed earlier I did not include the localize function inside of the my_enqueue function, but I still got a “my_ajax_obj not defined” after it was moved. I am not sure what could be going wrong. Right now my ajax call is in a root directory file called single-tutorials.php and the ajax script enqueueing and localize function is in my functions.php file.
This is what I have for my ajax script.
$.post(my_ajax_obj.ajax_url, { _ajax_nonce: my_ajax_obj.nonce, action: "lesson_watched", lastValue_php: lastValue });
And this is what I have in my functions.php file.
add_action( 'admin_enqueue_scripts', 'my_enqueue' ); function my_enqueue() { wp_enqueue_script( 'ajax-script', plugins_url( '/single-tutorials.php', __FILE__ ), array( 'jquery' ), '1.0.0', true ); $title_nonce = wp_create_nonce( 'lesson_nonce' ); wp_localize_script( 'ajax-script', 'my_ajax_obj', array( 'ajax_url' => '/wp-admin/admin-ajax.php', 'nonce' => $title_nonce, ) ); }
Do you see anything that is out of place? Thank you again for your help with this.
I apologize, but I have a few more questions. I was reading through the documentation for Server-Side PHP and Enqueueing and tried it, but I keep getting a console error saying that my_ajax_obj is not defined. Would you mind checking what I have so far?
For the enqueueing portion, I added this code for the AJAX script. I currently have my AJAX call set up in a php file called ‘single-tutorials.’ I am also using a custom theme, so I am not sure if that would cause an issue with the plugins_url?
add_action( 'admin_enqueue_scripts', 'my_enqueue' ); function my_enqueue( $hook ) { wp_enqueue_script( 'ajax-script', plugins_url( 'single-tutorials.php', __FILE__ ), array( 'jquery' ), '1.0.0', true ); }
I also made sure to create a nonce using the following code.
$title_nonce = wp_create_nonce( 'lesson_nonce' );
From there, I used wp_localize_script to define my_ajax_obj for the ajax script.
wp_localize_script( 'ajax-script', 'my_ajax_obj', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => $title_nonce, ) );
Finally, what I did was I wrote a very simple php function and put it in my index.php file.
add_action('wp_ajax_lesson_watched', 'lesson_added') function lesson_added() { $lastValue_php = wp_unsplash( $_POST['lastValue_php'] ); echo 'Howdy partner :)'; wp_die(); // Ends all AJAX handlers }
I should also note, the following code is the AJAX code I have in my single-tutorials.php file that triggers after a video is watched; ‘lastValue’ is a string class value passed from the video watched.
$.post(my_ajax_obj.ajax_url, { _ajax_nonce: my_ajax_obj.nonce, // nonce action: "lesson_watched", // action call lastValue_php: lastValue // data });
Thank you for all of your help with this, it is appreciated!
Hello,
Both of your responses make sense, and bcworkz, I read the link that you sent. I feel that helped put the pieces of what I need in a better perspective, however, if I wanted to trigger a PHP code to run, let’s say, taking that js data value and triggering a PHP code to add that value to a user’s meta data. How would I activate that code using AJAX? In the plugin handbook that bcworkz sent, it appears the function where the current title was removed and replaced was in js.
Thank you for your time,
IanHello,
I am using the up to date wishlist.php file. Initially, I got the code for a shortcode that you posted a couple of months ago for enrolled courses, but I modified so that I could get the look that I was hoping to achieve.
Now what I am trying to do is instead of using the enrolled courses for a user, I would like to gather a user’s wishlist courses instead. What I am stuck on is how you referenced a user’s wishlist courses to be able to display them. That what I am trying to figure out using the wishlist.php file; I saw the get_wishlist function in the wishlist.php file, but it only has null, and then two pagination variables which I don’t need because I would like to not include pagination.
What I am hoping you can answer is what input would I need for the get_wishlist function to pull a user’s wishlist courses? I tried using the user’s ID, but the site errored unfortunately. Thank you in advance.
Thank you for your time,
IanHello there,
Would you have an updated version of this code somewhere? I am not familiar with all of the class containers to rebuild the shortcode. Could it be adapted to fit with the new criteria?
Thank you for your time,
IanThank you so much; worked like a charm!