Forum Replies Created

Viewing 15 replies - 1 through 15 (of 17 total)
  • Thread Starter janmoes

    (@janmoes)

    So should I make like a switch, where I loop through all the inputs and check on the ID?
    Also how in that loop with my current code, could I change it so that atleast one radio button per radio button group needs to be required?

    Thread Starter janmoes

    (@janmoes)

    @a2hostingrj I’ve seen and tried that way yeah.

    In my example I just loop through all inputs. Which means it’s also going to loop through all name="gender" radio’s for example. The validation works, but it prints it for example two times, because of this, while I only need one validation message.

    Thread Starter janmoes

    (@janmoes)

    I was able to fix it. Normally I use jQuery, and I figured out, that jQuery sends data like something as FormData or URLSearchParams. So now I made an object filled with my fields and converted those in a URLSearchParams object!

    Thread Starter janmoes

    (@janmoes)

    Yeah I thought so too. If I register the Post Type in the activation hook, it would not exist after activating and refreshing the page. The content is being saved in the db, so that’s okay. But as I understand Init is a hook that runs on every request?

    And which removal hook were you talking about?

    Thread Starter janmoes

    (@janmoes)

    I managed to fix it like this:

    register_activation_hook(__FILE__, 'activate_cpt_locations');
    function activate_cpt_locations(){
    	$locations = [
    		[
    		  'post_title'   => 'Amsterdam',
    			'post_content' => '',
    			'post_status'  => 'publish',
    			'post_type'    => 'location',
    			'meta_input'   => [
    				'_location_id' => 13529,
    				'_sportivity_api_key'   => 123456,
    				'_clubplanner_username' => 'user',
    				'_clubplanner_password' => 'pass',
    			],
    		],
    		[
    		  'post_title'   => 'Delft',
    			'post_content' => '',
    			'post_status'  => 'publish',
    			'post_type'    => 'location',
    			'meta_input'   => [
    				'_location_id' => 13529,
    				'_sportivity_api_key'   => 123456,
    				'_clubplanner_username' => 'user',
    				'_clubplanner_password' => 'pass',
    			],
    		],
    	];
    	
    	foreach($locations as $location){
    		wp_insert_post($location);
    	}
    }
    
    register_deactivation_hook(__FILE__, 'deactivate_cpt_locations');
    function deactivate_cpt_locations(){
    	$locations = get_posts(['post_type'=>'location','numberposts'=>-1]);
    	foreach ($locations as $location) {
    		wp_delete_post( $location->ID, true );
    	}
    }
    
    add_action('init', 'register_location_cpt');
    function register_location_cpt(){
    	$labels = array(
     		'name' => 'Location',
        	'singular_name' => 'Location',
        	'add_new' => 'Add New Location',
        	'add_new_item' => 'Add New Location',
        	'edit_item' => 'Edit Location',
        	'new_item' => 'New Location',
        	'all_items' => 'All Locations',
        	'view_item' => 'View Location',
        	'search_items' => 'Search Locations',
        	'not_found' =>  'No Locations Found',
        	'not_found_in_trash' => 'No Locations found in Trash', 
        	'parent_item_colon' => '',
        	'menu_name' => 'Location',
        );
        //register post type
    	register_post_type( 'Location', array(
    		'labels' => $labels,
    		'has_archive' => true,
     		'public' => true,
    		'supports' => array( 'title', 'thumbnail','page-attributes'),
    		'taxonomies' => array( 'post_tag', 'category' ),	
    		'exclude_from_search' => false,
    		'capability_type' => 'post',
    		'rewrite' => array( 'slug' => 'locations' ),
    		)
    	);
    }

    Is it correct that the locations are inserted in the activation hook? And that the post type is made in the init hook?

    Thread Starter janmoes

    (@janmoes)

    shouldn’t I loop through $email[‘CustomerResources’] since I convert the object as an array?

    Thread Starter janmoes

    (@janmoes)

    Ah okay, cool!
    Do you have an example?

    Thread Starter janmoes

    (@janmoes)

    What kind of table from wordpress standards would you prefer?

    Thread Starter janmoes

    (@janmoes)

    It’s for an api, for the rest it has nothing to do with WP. So that’s probably why I did it that way.

    Thread Starter janmoes

    (@janmoes)

    Managed to fix it. Apparently you don’t need a form to perfom an ajax request. as long as you send an action with the request which has the same name as the action in your php file!

    Thread Starter janmoes

    (@janmoes)

    Oh that <code> part somehow happened when posting this issue.
    Otherwise, if that’s not what you mean. I’m wondering how else you can generate a table.
    I would like to learn some more about it too! 🙂

    • This reply was modified 4 years, 7 months ago by janmoes.
    Thread Starter janmoes

    (@janmoes)

    Yeah my dumb fault, it was a long day and I was tired. I literally forgot a ‘.’ in front of 'class.php' to get classes like location.class.php

    Thread Starter janmoes

    (@janmoes)

    I made my own custom plugin, and need to generate a table for api results.
    Is there an other way besides installing a plugin for it or so?

    Thread Starter janmoes

    (@janmoes)

    Yeah I actually thought about. In the backend you either return the success message like “Your form has been submitted” or the list of errors. This can be done with some simple PHP logic, to let the system know what you want to return. And ofcourse what you said. Using the error/catch for server errors like a 4xx.

    Thread Starter janmoes

    (@janmoes)

    Didn’t know about that wp_send_json method :), I started working on developing plugins a week ago. But I already made the serverside validation. I’d like to learn how to imply that with my jquery/ajax code for future purposes etc.

    With not finding a good example with my needs, I meant that a lot of small tutorials all look so different.

    I was trying to find a tutorial where they could send like an entire $errors array to the catch/error method of an ajax request.

Viewing 15 replies - 1 through 15 (of 17 total)