traveler
Forum Replies Created
-
You can do that with Gravity Forms because they allow you to create polls and surveys with the elite plugin level. You would then need to allow a user to create a form on the front-end using Javascript and REST.
If any of the plugins listed above in conleywilliam32’s link use the REST API then you could do the same with them. I do not know of a ready-built solution though to allow site users to create them from scratch.
Forum: Developing with WordPress
In reply to: Custom meta box with loop for post edit screenThis adjustment seems to have fixed the above errros:
<?php // ADDS META BOX TO 'CLOSET' SCREEN SHOWING OUTFITS CREATED FOR THIS CLIENT // ADDS FONT AWESOME TO 'CLOSET' ADMIN SCREEN function queue_scripts_to_closet() { wp_enqueue_style( 'le-font-awesome-css', 'https://pro.fontawesome.com/releases/v5.10.0/css/all.css' ); } add_action('admin_enqueue_scripts', 'queue_scripts_to_closet'); // CREATE THE META BOX function le_outfit_loop_meta_box() { $screens = ['closet']; foreach ($screens as $screen) { add_meta_box( 'outfit-loop-meta-wrapper', // Unique ID 'Outfit History', // Box title 'le_outfit_loop_html', // Content callback function, must be of type callable $screen, 'side' ); } } add_action('add_meta_boxes_closet', 'le_outfit_loop_meta_box'); // HTML TO RENDER THE META BOX function le_outfit_loop_html($post) { global $post; $tempPostVar = $post; $editScreenId = $post->ID; $clientTaxTerm = $post->post_title; echo "<h4>" . $clientTaxTerm . "'s Outfits:</h4>"; ?> <div class="admin-button-wrapper"> <a href="/wp-admin/post-new.php?post_type=outfit" id="js-create-outfit-btn" class="btn"> <i class="fas fa-plus-circle"> </i> Create New Outfit </a> </div> <?php $outfitLoopArgs = array( 'post_type' => 'outfit', 'tax_query' => array( array( 'taxonomy' => 'client', 'field' => 'slug', 'terms' => $clientTaxTerm, // should equal the closet post title ), ), ); $outfits = get_posts( $outfitLoopArgs ); if ($outfits) : ?> <ol> <?php foreach ($outfits as $outfit): setup_postdata( $outfit ); ?> <li> <a target="_blank" href="<?php echo get_edit_post_link($outfit->ID); ?>"><?php echo $outfit->post_title; ?></a> </li> <?php endforeach; // wp_reset_postdata(); $post = $tempPostVar; ?> </ol> <?php endif; }Forum: Developing with WordPress
In reply to: Add wp_query loop to cpt admin screenThanks bcworkz, that was exactly what I was looking for. I did not know they were all essentially meta boxes.
This solution worked for me:
https://nabtron.com/error-compilation-failed-invalid-range-character-class-offset/
Yeah I get the same message and it happens on every save.
Thanks.
Forum: Developing with WordPress
In reply to: Update CPT with custom fields with REST APIHey bcworkz, that worked. Funny thing, I guess ‘single’ defaults to false, because before I added it previously I was having the issue of not all the words in a string being saved. However, adding it as single=>true did fix this issue.
Thanks for your advice.
Forum: Developing with WordPress
In reply to: Update CPT with custom fields with REST APIUpdate, I changed a lot around, now I have it working almost. The only issue is the fields that have spaces are being sent as strings but returned as arrays so only the first word of that string shows without adjusting for the return array value in the page template. I imagine this has to do with me not using
register_rest_route()?Here is my jquery:
updateProfile() { this.title = this.firstName.val() + ' ' + this.lastName.val(); this.slug = this.firstName.val() + ' - ' + this.lastName.val(); this.firstName = this.firstName.val(); this.lastName = this.lastName.val(); this.email = this.email.val(); this.cellPhone = this.cellPhone.val(); this.homePhone = this.homePhone.val(); this.officePhone = this.officePhone.val(); this.homeAddress = this.homeAddress.val(); this.city = this.city.val(); this.state = this.state.val(); this.zipCode = this.zipCode.val(); this.gender = this.gender.val(); this.dateOfBirth = this.dateOfBirth.val(); this.rolodexId = this.rolodexId.val(); this.staffId = this.staffId.val(); this.department = this.department.val(); this.jobTitle = this.jobTitle.val(); this.supervisorsFirstName = this.supervisorsFirstName.val(); this.supervisorsLastName = this.supervisorsLastName.val(); this.userProfileIntro = this.userProfileIntro.val(); $.ajax({ beforeSend: (xhr) => { xhr.setRequestHeader('X-WP-Nonce', leadershipData.nonce); }, url: leadershipData.root_url + '/wp-json/wp/v2/profiles/' + this.profileWrapper.data('id'), type: 'PUT', data: { title: this.title, slug: this.slug, meta: { first_name: this.firstName, last_name: this.lastName, email_address: this.email, cell_phone: this.cellPhone, home_phone: this.homePhone, office_phone: this.officePhone, home_address: this.homeAddress, city: this.city, state: this.state, zip_code: this.zipCode, gender: this.gender, date_of_birth: this.dateOfBirth, rolodex_id: this.rolodexId, staff_id: this.staffId, department: this.department, job_title: this.jobTitle, supervisors_first_name: this.supervisorsFirstName, supervisors_last_name: this.supervisorsLastName, user_profile_page_intro: this.userProfileIntro } }, success: (response) => { this.makeProfileReadOnly(this.inputFields); console.log('Congrats a success!'); console.log(response); }, error: (response) => { console.log('No Sorry'); console.log(response); } }); }I have my PHP custom fields registered in this format:
register_meta('post', 'first_name', [ 'object_subtype' => 'profile', 'show_in_rest' => true, 'type' => 'string', ]); register_meta('post', 'last_name', [ 'object_subtype' => 'profile', 'show_in_rest' => true, 'type' => 'string' ]); register_meta('post', 'email_address', [ 'object_subtype' => 'profile', 'show_in_rest' => true, 'type' => 'string' ]);Using
'type' => 'string'did not function as I thought, which was that it would force that value to be a string.What is the next step to look into so that my input fields and text fields with multiple words within them aren’t converted to arrays before being sent back to the page?
Forum: Developing with WordPress
In reply to: Update CPT with custom fields with REST APIThanks for your reply. I replaced the ‘register_rest_field()’ with this format:
register_meta('post', 'first_name', [ 'object_subtype' => 'profile', 'show_in_rest' => true ]);And it shifted how the data was displayed in json as expected to:
{ id: 434, date: "2020-04-01T22:36:41", date_gmt: "2020-04-01T22:36:41", guid: { rendered: "http://devleadership.staging.wpengine.com/?post_type=profile&p=434" }, modified: "2020-05-19T18:55:18", modified_gmt: "2020-05-19T18:55:18", slug: "hansel-gretel", status: "publish", type: "profile", link: "http://devleadership.staging.wpengine.com/profile/hansel-gretel/", title: { rendered: "Hansel Gretel" }, template: "", meta: { first_name: [ "Stephen" ], last_name: [ "Jones" ], email_address: [ "sjones@yahoo.com" ], cell_phone: [ "322-333-3333" ],…and down the list of custom fields using that same pattern. It still behaves the same way, I get a success message and only the title and slug update with the ACF values for first_name and last_name, but the actual fields displaying first name and last name do not update, they revert back to what they were.
Am I trying to access the post meta fields incorrectly in the Ajax call?
updateProfile() { const ourUpdatedPost = { 'title': this.profileWrapper.find('input[name="first-name"]').val() + ' ' + this.profileWrapper.find('input[name="last-name"]').val(), 'slug': this.profileWrapper.find('input[name="first-name"]').val() + '-' + this.profileWrapper.find('input[name="last-name"]').val(), 'first_name': this.profileWrapper.find('input[name="first-name"]').val(), 'last_name': this.profileWrapper.find('input[name="last-name"]').val(), 'email': this.profileWrapper.find('input[name="email-address"]').val(), 'cell_phone': this.profileWrapper.find('input[name="cell-phone"]').val(), 'home_phone': this.profileWrapper.find('input[name="home-phone"]').val(), 'office_phone': this.profileWrapper.find('input[name="office-phone"]').val(), 'home_address': this.profileWrapper.find('input[name="home-address"]').val(), 'city': this.profileWrapper.find('input[name="city"]').val(), 'state': this.profileWrapper.find('input[name="state"]').val(), 'zip_code': this.profileWrapper.find('input[name="zip-code"]').val(), 'gender': this.profileWrapper.find('input[name="gender"]').val(), 'date_of_birth': this.profileWrapper.find('input[name="date-of-birth"]').val(), 'rolodex_id': this.profileWrapper.find('input[name="rolodex-id"]').val(), 'staff_id': this.profileWrapper.find('input[name="staff-id"]').val(), 'department': this.profileWrapper.find('input[name="department"]').val(), 'job_title': this.profileWrapper.find('input[name="job-title"]').val(), 'supervisors_first_name': this.profileWrapper.find('input[name="supervisors-first-name"]').val(), 'supervisors_last_name': this.profileWrapper.find('input[name="supervisors-last-name"]').val(), 'user_profile_intro': this.profileWrapper.find('textarea[name="user-profile-intro"]').val() } console.log(ourUpdatedPost); $.ajax({ beforeSend: (xhr) => { xhr.setRequestHeader('X-WP-Nonce', leadershipData.nonce); }, url: leadershipData.root_url + '/wp-json/wp/v2/profiles/' + this.profileWrapper.data('id'), type: 'PUT', data: ourUpdatedPost, success: (response) => { this.makeProfileReadOnly(this.inputFields); console.log('Congrats a success!'); console.log(response); }, error: (response) => { console.log('No Sorry'); console.log(response); } }); }Forum: Fixing WordPress
In reply to: Underscores – responsive menuYou don’t need the span element, I just like to have it say ‘Menu’ next to the hamburger sometimes. You could omit the entire thing out if you’d like.
This php
<?php esc_html_e( 'Primary Menu', 'my-slug' ); ?>in between the button just pulls in the menu name, so you could omit that. You’ll need to remove the CSS around the button that gives the background and the border. Try putting the font awesome icon inside the button and make sure that no CSS styles prevent it from showing. See below for font awesome.The element from my first post that makes the hamburger is the i with class fa fa-bars, this is for the font-awesome library and the class calls the correct image for what you’re wanting. This would require you to enqueue the font-awesome library in your functions.php. Here is the font-awesome page for the bars: font awesome
It probably didn’t work if you just tried dropping my code in there, the jQuery with underscores is looking for button with class menu-toggle, so that when clicked it has a class added to it via jQuery which causes the menu container to “toggle” to be on. It’s been a while since I looked at it in depth, but I believe it adds a class of “toggled” and that changes the CSS for the container div to be hidden or placed way offscreen, to that of visible or having it slide into the screen. That’s all done with CSS and the jquery part will just add/remove the classes to enable this to happen.
Forum: Fixing WordPress
In reply to: Underscores – responsive menuI believe that it is responsive with the base underscores theme install. However, I do like to customize my mobile menu’s more than the default so I typically strip out their mobile and use my own.
Are you familiar with CSS and simple jQuery to addClass and removeClass? Create your mobile menu in a div, put your menu code inside it or I like to use a plugin that allows me to use menu shortcodes in theme pages. Then hide this div and it’s contents when the page loads and then use jQuery to add a class of ‘visible’ or whatever you call it, that has CSS for display:block or whatever. I prefer a full screen mobile menu, so I do
height:100vh; width:100%; position:absolute; top:0; left:0;For the actual hamburger looking part, that’s html:
<button id="js-menu-toggle" class="menu-toggle" aria-controls="primary-menu" aria-expanded="false"> <i id="offscreen-toggle-icon" class="fas fa-bars"></i> <span class="menu-text">MENU</span> </button>I also like to keep the hamburger visible sometimes on desktop in addition to a top nav just so the hamburger is consistently in the same place. Depends on the project though.
Forum: Developing with WordPress
In reply to: Create/edit/delete CPT w/RESTOk, I was under the impression these routes were automatically generated when you “show in rest” for the CPT. Here’s how I’ve created these:
<?php // second param is function name creating the routes add_action('rest_api_init', 'eventRestRoute'); function eventRestRoute() { // first two args can be whatever register_rest_route('seiu-events/v1', 'seiuEventsRest', array( 'methods' => 'POST', 'callback' => 'createEvent' )); register_rest_route('seiu-events/v1', 'seiuEventsRest', array( 'methods' => 'DELETE', 'callback' => 'deleteEvent' )); register_rest_route('seiu-events/v1', 'seiuEventsRest', array( 'methods' => 'PUT', 'callback' => 'editEvent' )); } // create new event post function createEvent() { // create the event post wp_insert_post(array( 'post_type' => 'events', 'post_status' => 'publish', 'post_title' => 'Rest Post', 'post_content' => 'This is an amazing post.' )); }At this point I’m not attempting to capture any of the json data sent but just create a new Events post titled “Rest Post” to make sure I’m going in the correct direction.
When I use Postman to make a POST request to
example.com/wp-json/wp/v2/events, I get this error:{ "code": "rest_cannot_create", "message": "Sorry, you are not allowed to create posts as this user.", "data": { "status": 401 } }Reading up on the REST API documentation I see that the cookie authentication that uses nonce only works for using the REST API from within wordpress. My calls are coming from outside of WordPress. What is the workaround for this, assuming my code above should allow a simple post to be created just by sending data to my custom route?
Last, am I wrong to assume I do not need to be using Ajax for this, since the request is not originating in a browser but instead from an outside system?
Forum: Developing with WordPress
In reply to: Create/edit/delete CPT w/RESTQuick follow-up to my question about what triggers this to create a post, do I need the URL that sends the POST request with the above json to go directly to the php file that creates the post?
This site had something like this several years ago that posted to:
example.com/wp-content/themes/SEIU/setup/events/add-event.php?auth=seiujLJ62xwith a simple auth check function before processing the json to create the new custom post?
Forum: Developing with WordPress
In reply to: Create/edit/delete CPT w/RESTSo I’ve been reading as much as possible on this, going over tuts, but most all that I’ve seen talks about using the rest api to query it’s own database, meaning in my theme I query my site’s database to fetch data and create posts from it. In all these, there is a javascript trigger that starts it.
I’m now confused now though, about what triggers making/creating a post when JSON data is sent to an endpoint?
I’ve created and deleted cpt’s using wp_insert_post() that creates a post with a title that matches an ID number attached to the end of a URL. In this case, if that ID exists, then I use ajax to send that ID to PHP and call a PHP function that then uses wp_insert_post() to create the post. To delete it, I use the same ID as a meta_value to query the correct post and then use wp_delete_post() to delete it. This is a very simple tracking mechanism to see who goes to a signup form and then doesn’t complete it.
So what am I missing here, I have a small JSON object that will be sent to my route,
example.info/wp-json/wp/v2/events
and my JSON that will be sent is:{ "eventID": 16924, "regions": "R1, R2, R3", "type": "Rally", "eventName": "Test Event Name", "eventLocation": "Test Location", "eventStartDate": "2/29/2020", "eventStartTime": "12:00", "eventStartAMPM": "pm", "eventEndTime": "2:00", "eventEndAMPM": "pm", "staffResponsible": "Staffname1", "notes": "", "counties": "Los Angeles, San Bernardino", "dept": "Information Technology" }So what will trigger my function to create a post from this?
Forum: Developing with WordPress
In reply to: Create/edit/delete CPT w/RESTHey bcworkz,
You bring up a very good point – thank you. So are you saying it would be better and potentially easier to simply query this information on page load, with the page being loaded the “events” category feed, from the company’s database that contains this information?How would querying the events in this fashion affect page caching for load times? Fwiw, the site is hosted on WPengine.
Thanks.
- This reply was modified 6 years, 3 months ago by traveler.