Can I change the jquery to plain js and how do I send the errors back to the form

Hello, I have this challenge Task: Develop a shortcode that will output a simple contact form. This contact form, when submitted, will use wp_mail to send the email. Requirements: Shortcode needs to have fields: Subject (input), Email (input), Message (textarea), Sanitize all fields before being used for sending it, Use wp_mail to send the email Style it and enqueue style only on pages where the shortcode is Display errors on top of the form I found a tutorial that does almost that but it works with jquery. Now my question are : 1) is there a way I can convert the jquery to plain js. 2) How do I make it work that if there is a validation error I can show it also above the form. Code of the tutorial : https://gitlab.com/roelofwobben/short-code-form ping @ἔρως
3171 Replies
roelof
roelofOP15mo ago
you do not have the make it all but if you can let me see how things could work i will be happy
ἔρως
ἔρως15mo ago
well, submit_contact_form can be way better written https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData#form <-- this accepts a form and does all the work for you https://gitlab.com/roelofwobben/short-code-form/-/blob/main/mycustomForm.php?ref_type=heads#L22 <-- why did you remove the form? also, this should be inside a view https://developer.wordpress.org/reference/functions/load_template/ if you check the source code, you can see that it basically just does a require $_template_file; it also has the wp_before_load_template hook, which probably can be used to enqueue your javascript code and styles you're also submitting the form to an url inside a plugin instead, you can submit to the current page, or use the ajax file
roelof
roelofOP15mo ago
I deleted the form because the author of the video said we do not need it because we are using ajax
ἔρως
ἔρως15mo ago
that's the dumbest thing i read all day
roelof
roelofOP15mo ago
oke
ἔρως
ἔρως15mo ago
you need the form
roelof
roelofOP15mo ago
so back to the drawing table 😢
ἔρως
ἔρως15mo ago
that author is drunk
roelof
roelofOP15mo ago
or rewrite a lot
ἔρως
ἔρως15mo ago
rewrite a nice chunk, yes but it's not that hard
roelof
roelofOP15mo ago
can we do that piece by piece
ἔρως
ἔρως15mo ago
yes, we can
roelof
roelofOP15mo ago
so first step is to make the form tag back ? second thing would be i think find out how I can make the action to say I need the process file I think
ἔρως
ἔρως15mo ago
no no
roelof
roelofOP15mo ago
I will not do that for now. Tired after a very ad night
ἔρως
ἔρως15mo ago
lets start with the form
roelof
roelofOP15mo ago
oke, so use the form tag Easy fix
ἔρως
ἔρως15mo ago
kinda because whoever's tutorial you read was just waffling about and doing the dumbest things also, everything has the name of the tutorial add_shortcode('contact_form','ideapro_contact_form'); <-- like this instead of ideapro, use something unique to you for example, ideapro_contact_form could be roelof_contact_form then create a folder called "templates" and create a file called "form.php" replace the whole thing with this:
function roelof_contact_form() {
ob_start();
load_template(__FILE__ . '/templates/form.php', false, func_get_args());
return ob_end_flush();
}

add_shortcode('contact_form','roelof_contact_form');
function roelof_contact_form() {
ob_start();
load_template(__FILE__ . '/templates/form.php', false, func_get_args());
return ob_end_flush();
}

add_shortcode('contact_form','roelof_contact_form');
now, you have a php file that can output the form you should add the shortcode inside an init hook, like this:
function roelof_add_custom_shortcode() {
function roelof_contact_form() {
ob_start();
load_template(__FILE__ . '/templates/form.php', false, func_get_args());
return ob_end_flush();
}

add_shortcode('contact_form', 'roelof_contact_form');
}
add_action('init', 'roelof_add_custom_shortcode');
function roelof_add_custom_shortcode() {
function roelof_contact_form() {
ob_start();
load_template(__FILE__ . '/templates/form.php', false, func_get_args());
return ob_end_flush();
}

add_shortcode('contact_form', 'roelof_contact_form');
}
add_action('init', 'roelof_add_custom_shortcode');
that should output the form
roelof
roelofOP15mo ago
oke and the form template schould include a form tag So I have to find out what the action then should be
ἔρως
ἔρως15mo ago
it's not a "should" it's a "must" a form without a form isn't a form it's just a bunch of disjointed fields with no meaning at all
roelof
roelofOP15mo ago
oke So form.php must look like this :
<?php

$content = ''; /* create a string */

$content .= '<form method="post" action="" >';

$content .= '<style>.ideapro_form label { display:block; padding:15px 0px 4px 0px; } .ideapro_form input[type=text],input[type=email] { width:400px; padding:8px; } .ideapro_form textarea { width:400px; height:200px; padding:8px;}</style>';

$content .= '<div id="response_div"></div>';

$content .= '<div class="ideapro_form">';
$content .= '<label for="your_name">Your Name</label>';
$content .= '<input type="text" name="your_name" id="your_name" placeholder="Your Full Name" />';

$content .= '<label for="your_email">Your Email Address</label>';
$content .= '<input type="email" name="your_email" id="your_email" placeholder="Enter Your Email Address" />';

$content .= '<label for="phone_number">Phone Number</label>';
$content .= '<input type="text" name="phone_number" id="phone_number" placeholder="Your Phone Number" />';

$content .= '<label for="your_comments">Questions or Comments</label>';
$content .= '<textarea name="your_comments" id="your_comments" placeholder="Say something nice"></textarea>';

$content .= '<br /><br /><input type="submit" name="ideapro_contact_submit" id="ideapro_contact_submit" onclick="submit_contact_form()" value="SUBMIT YOUR INFORMATION" />';

$content .= '</div>';

$content .= '</form>';

return $content;
<?php

$content = ''; /* create a string */

$content .= '<form method="post" action="" >';

$content .= '<style>.ideapro_form label { display:block; padding:15px 0px 4px 0px; } .ideapro_form input[type=text],input[type=email] { width:400px; padding:8px; } .ideapro_form textarea { width:400px; height:200px; padding:8px;}</style>';

$content .= '<div id="response_div"></div>';

$content .= '<div class="ideapro_form">';
$content .= '<label for="your_name">Your Name</label>';
$content .= '<input type="text" name="your_name" id="your_name" placeholder="Your Full Name" />';

$content .= '<label for="your_email">Your Email Address</label>';
$content .= '<input type="email" name="your_email" id="your_email" placeholder="Enter Your Email Address" />';

$content .= '<label for="phone_number">Phone Number</label>';
$content .= '<input type="text" name="phone_number" id="phone_number" placeholder="Your Phone Number" />';

$content .= '<label for="your_comments">Questions or Comments</label>';
$content .= '<textarea name="your_comments" id="your_comments" placeholder="Say something nice"></textarea>';

$content .= '<br /><br /><input type="submit" name="ideapro_contact_submit" id="ideapro_contact_submit" onclick="submit_contact_form()" value="SUBMIT YOUR INFORMATION" />';

$content .= '</div>';

$content .= '</form>';

return $content;
` or must I change the $content to a echo ? and what will be the action ?
ἔρως
ἔρως15mo ago
no, just pure html in that file the action can change but, for now, just leave it at . or javascript:void(0)
roelof
roelofOP15mo ago
oke, changed everything that you asked for DO you need to check it so I "have" to put the new code on gitlab
ἔρως
ἔρως15mo ago
would be great, yes
roelof
roelofOP15mo ago
done
ἔρως
ἔρως15mo ago
much better now, style it how you want it
roelof
roelofOP15mo ago
oke, maybe change it to the form I really wanted Then I can use the css in the css directory
ἔρως
ἔρως15mo ago
just make it look how you want it
roelof
roelofOP15mo ago
oke moment, I have to check if it is looking as I wanted oops. I made somewhere a error :
Warning: require(C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\mycustomForm.php/templates/form.php): Failed to open stream: No such file or directory in C:\laragon\www\myBlog\wp-includes\template.php on line 792

Fatal error: Uncaught Error: Failed opening required 'C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\mycustomForm.php/templates/form.php' (include_path='.;C:/laragon/etc/php/pear') in C:\laragon\www\myBlog\wp-includes\template.php:792 Stack trace: #0 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\mycustomForm.php(10): load_template('C:\\laragon\\www\\...', false, Array) #1 C:\laragon\www\myBlog\wp-includes\shortcodes.php(433): roelof_contact_form(Array, '', 'contact_form') #2 [internal function]: do_shortcode_tag(Array) #3 C:\laragon\www\myBlog\wp-includes\shortcodes.php(273): preg_replace_callback('/\\[(\\[?)(contac...', 'do_shortcode_ta...', '<!-- wp:templat...') #4 C:\laragon\www\myBlog\wp-includes\block-template.php(231): do_shortcode('<!-- wp:templat...') #5 C:\laragon\www\myBlog\wp-includes\template-canvas.php(12): get_the_block_template_html() #6 C:\laragon\www\myBlog\wp-includes\template-loader.php(106): include('C:\\laragon\\www\\...') #7 C:\laragon\www\myBlog\wp-blog-header.php(19): require_once('C:\\laragon\\www\\...') #8 C:\laragon\www\myBlog\index.php(17): require('C:\\laragon\\www\\...') #9 {main} thrown in C:\laragon\www\myBlog\wp-includes\template.php on line 792
Warning: require(C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\mycustomForm.php/templates/form.php): Failed to open stream: No such file or directory in C:\laragon\www\myBlog\wp-includes\template.php on line 792

Fatal error: Uncaught Error: Failed opening required 'C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\mycustomForm.php/templates/form.php' (include_path='.;C:/laragon/etc/php/pear') in C:\laragon\www\myBlog\wp-includes\template.php:792 Stack trace: #0 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\mycustomForm.php(10): load_template('C:\\laragon\\www\\...', false, Array) #1 C:\laragon\www\myBlog\wp-includes\shortcodes.php(433): roelof_contact_form(Array, '', 'contact_form') #2 [internal function]: do_shortcode_tag(Array) #3 C:\laragon\www\myBlog\wp-includes\shortcodes.php(273): preg_replace_callback('/\\[(\\[?)(contac...', 'do_shortcode_ta...', '<!-- wp:templat...') #4 C:\laragon\www\myBlog\wp-includes\block-template.php(231): do_shortcode('<!-- wp:templat...') #5 C:\laragon\www\myBlog\wp-includes\template-canvas.php(12): get_the_block_template_html() #6 C:\laragon\www\myBlog\wp-includes\template-loader.php(106): include('C:\\laragon\\www\\...') #7 C:\laragon\www\myBlog\wp-blog-header.php(19): require_once('C:\\laragon\\www\\...') #8 C:\laragon\www\myBlog\index.php(17): require('C:\\laragon\\www\\...') #9 {main} thrown in C:\laragon\www\myBlog\wp-includes\template.php on line 792
Should File not be Dir ?

load_template(__FILE__ . '/templates/form.php', false, func_get_args());

load_template(__FILE__ . '/templates/form.php', false, func_get_args());
ἔρως
ἔρως15mo ago
yes, should be DIR __DIR__
roelof
roelofOP15mo ago
oke, Form looks well now and updated the repo What I still miss is the arguments of the shortcode I thought the purpose was to use [custom_form subject="I have something to say to you" ]
ἔρως
ἔρως15mo ago
yes, you can do that but, i made a boo-boo
roelof
roelofOP15mo ago
oke For me things looks to work well
ἔρως
ἔρως15mo ago
function roelof_add_custom_shortcode() {
function roelof_contact_form($atts, $content, $shortcode_tag) {
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode_tag' => $shortcode_tag,
]);
return ob_end_flush();
}

add_shortcode('contact_form', 'roelof_contact_form');
}
add_action('init', 'roelof_add_custom_shortcode');
function roelof_add_custom_shortcode() {
function roelof_contact_form($atts, $content, $shortcode_tag) {
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode_tag' => $shortcode_tag,
]);
return ob_end_flush();
}

add_shortcode('contact_form', 'roelof_contact_form');
}
add_action('init', 'roelof_add_custom_shortcode');
use this instead inside the template, you can access the $args variable, which will have the keys defided in this chunk
roelof
roelofOP15mo ago
oke right now not so very important I rather like to work on the validation part which I not understand well
ἔρως
ἔρως15mo ago
you can do var_dump($args) inside the template validation should be the last thing
roelof
roelofOP15mo ago
yes. it seems to work
array(3) { ["atts"]=> array(1) { ["subject"]=> string(1) "m" } ["content"]=> string(0) "" ["shortcode_tag"]=> string(12) "contact_form" }
array(3) { ["atts"]=> array(1) { ["subject"]=> string(1) "m" } ["content"]=> string(0) "" ["shortcode_tag"]=> string(12) "contact_form" }
ἔρως
ἔρως15mo ago
nice now, you can do something, inside the template file
roelof
roelofOP15mo ago
so I can value = " <?php echo $atts[' subject"] ?> " ?
ἔρως
ἔρως15mo ago
not yet at the top of the template, you can do $atts = array_replace([...], $args['atts']);
roelof
roelofOP15mo ago
only that
ἔρως
ἔρως15mo ago
inside the first array, put all the default values you want for all atts
roelof
roelofOP15mo ago
not have to do <?php before it ??
ἔρως
ἔρως15mo ago
yes, you have, but that's implied as well as the closing ?> before the html
roelof
roelofOP15mo ago
oke, done that
ἔρως
ἔρως15mo ago
can you commit it then?
roelof
roelofOP15mo ago
done
ἔρως
ἔρως15mo ago
😂 [...] you're supposed to add the default values there
roelof
roelofOP15mo ago
??? Did I not iisten well
ἔρως
ἔρως15mo ago
for example, if you want to take a subject, add the key there
roelof
roelofOP15mo ago
I want all three to be empty as default
ἔρως
ἔρως15mo ago
then you don't have to do anything but there's things you still need to do on the form by the way, don't add a title <div class="container"> <-- don't use this generic class you can allow setting the classes, and add a data-attribute to it something like this:
<div class="<?php echo $atts['class'] ?>" data-shortcode="<?php echo $args['shortcode_tag'] ?>">
<div class="<?php echo $atts['class'] ?>" data-shortcode="<?php echo $args['shortcode_tag'] ?>">
roelof
roelofOP15mo ago
oke, but I think I break then the css
ἔρως
ἔρως15mo ago
yes, you will break the css
roelof
roelofOP15mo ago
😦
ἔρως
ἔρως15mo ago
but then you use [data-shortcode="contact_form"] instead of the class
roelof
roelofOP15mo ago
🙂
ἔρως
ἔρως15mo ago
or, come up with better (and more unique) class names "container" is a very generic name, and will conflict with others i know some people have aversion to css attribute selectors
roelof
roelofOP15mo ago
not me but I made a typo I think somewhere :
Parse error: syntax error, unexpected token "]" in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\form.php on line 1
Parse error: syntax error, unexpected token "]" in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\form.php on line 1
ἔρως
ἔρως15mo ago
send the code here
roelof
roelofOP15mo ago
This part
<?php $atts = array_replace([...], $args['atts']); ?>
<?php $atts = array_replace([...], $args['atts']); ?>
the parser d not like the ] after the ...
ἔρως
ἔρως15mo ago
you need to add the class key on the first array, without the ...
roelof
roelofOP15mo ago
???
ἔρως
ἔρως15mo ago
[...] represents an array that should have the attributes you want to handle
roelof
roelofOP15mo ago
oke, so I have to add subject => "", email => "' , message = ""
ἔρως
ἔρως15mo ago
yes, and the class, if you decided to do what i said
roelof
roelofOP15mo ago
sorry, im still confused
ἔρως
ἔρως15mo ago
just do what you think im saying even if it is wrong
roelof
roelofOP15mo ago
it's very late here so im heading to bed Hopefully I have a good sleep after 3 - 4 bad nights
ἔρως
ἔρως15mo ago
yes, it's best to stop and rest by the way, if you use vscode, install the intelephense extension
roelof
roelofOP15mo ago
I use vs code for every coding job I have now this :
<?php $atts = array_replace(["subject" => "", "email" => "" , "message" => ""], $args['atts']); ?>
<?php $atts = array_replace(["subject" => "", "email" => "" , "message" => ""], $args['atts']); ?>
but that breaks the whole form
ἔρως
ἔρως15mo ago
it shouldn't if all you changed is that line, it shouldn't have done anything
roelof
roelofOP15mo ago
it does see this screenshot
roelof
roelofOP15mo ago
No description
roelof
roelofOP15mo ago
but as I said Im heading to bed I will commit the code so far
ἔρως
ἔρως15mo ago
that's the data attribute vs the class if you used this, then that result is expected
roelof
roelofOP15mo ago
oke, but as far as I know I also changed the css Thanks so far
ἔρως
ἔρως15mo ago
you're welcome
roelof
roelofOP15mo ago
and I hope we can work tomorrow further on this learned a lot and hopefully we can figure out what broke the form and if I make the first line well GN
ἔρως
ἔρως15mo ago
goodnight
roelof
roelofOP15mo ago
found something
<b>Warning</b>: Undefined array key " class"="" in="" <b="">C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\form.php on line <b>3</b><br>
" data-shortcode="contact_form"&gt;


<b>Warning</b>: Undefined array key " class"="" in="" <b="">C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\form.php on line <b>3</b><br>
" data-shortcode="contact_form"&gt;


See you hopefully tomorow
ἔρως
ἔρως15mo ago
you need to add it to the array https://wordpress.stackexchange.com/questions/165754/enqueue-scripts-styles-when-shortcode-is-present <-- this will be important for tomorrow, but DONT IMPLEMENT IT YET
roelof
roelofOP15mo ago
oke bosss and I wonder if we should not santize the data before we are going to display the data This seems to work but I do not know if it's right
<?php $atts = array_replace(["class" => "" , "subject" => "", "email" => "" , "message" => ""], $args['atts']); ?>
<?php $atts = array_replace(["class" => "" , "subject" => "", "email" => "" , "message" => ""], $args['atts']); ?>
but if I have to do it, I would use the has_shortcode and have to think about the first argument
ἔρως
ἔρως15mo ago
yes, but you sanitize when showing it you won't need to think about that
roelof
roelofOP15mo ago
oke I will wait on further instructions but is my atts line good or not ?
ἔρως
ἔρως15mo ago
it could be better formatted, but that's fine
roelof
roelofOP15mo ago
happy to hear Then I did that well
ἔρως
ἔρως15mo ago
you did now, you have to make it useful
roelof
roelofOP15mo ago
yep Like show the parts in the form I think I can use something like this :
value="<?php echo $atts['subject'] ?> "
value="<?php echo $atts['subject'] ?> "
ἔρως
ἔρως15mo ago
yes, but use the esc_attr function to output all attributes, including the data atttribute from yesterday
Andrew Nacin
WordPress Developer Resources
esc_attr() – Function | Developer.WordPress.org
Escaping for HTML attributes.
roelof
roelofOP15mo ago
oke So i have to look like this :
value="<?php echo esc_attr($atts['subject']) ?> "

value="<?php echo esc_attr($atts['subject']) ?> "

?
ἔρως
ἔρως15mo ago
yup
roelof
roelofOP15mo ago
done commit it so you can check things ?
ἔρως
ἔρως15mo ago
always commit and it's not because of me
roelof
roelofOP15mo ago
oke, committed and pushed
ἔρως
ἔρως15mo ago
good now, remove that h1 your form shouldn't have to handle titles
roelof
roelofOP15mo ago
done
ἔρως
ἔρως15mo ago
value="<?php echo esc_attr($atts['messsage']) ?> " > <-- by the way, don't do this
roelof
roelofOP15mo ago
moment, I think I made a typo on the message part
ἔρως
ἔρως15mo ago
remove all whitespace inside the value
roelof
roelofOP15mo ago
hmm, I have to dive into something I see now a error message that "message" cannot be found as a key
ἔρως
ἔρως15mo ago
show me the errors
roelof
roelofOP15mo ago
<textarea required="" value="<br />
<b>Warning</b>: Undefined array key " messsage"="" in="" <b="">C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\form.php&lt;/b&gt; on line &lt;b&gt;15&lt;/b&gt;&lt;br /&gt;
"&gt; </textarea>
<textarea required="" value="<br />
<b>Warning</b>: Undefined array key " messsage"="" in="" <b="">C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\form.php&lt;/b&gt; on line &lt;b&gt;15&lt;/b&gt;&lt;br /&gt;
"&gt; </textarea>
ἔρως
ἔρως15mo ago
messsage
roelof
roelofOP15mo ago
So this line
<textarea required value="<?php echo esc_attr($atts['messsage']) ?>"> </textarea>

<textarea required value="<?php echo esc_attr($atts['messsage']) ?>"> </textarea>

ἔρως
ἔρως15mo ago
just look at the key tip: double-click a word in vscode, and it will highlight all the places where that word shows up
roelof
roelofOP15mo ago
looks well now
ἔρως
ἔρως15mo ago
are you sure?
roelof
roelofOP15mo ago
yep
roelof
roelofOP15mo ago
see this screenshot :
No description
roelof
roelofOP15mo ago
form looks well to me
ἔρως
ἔρως15mo ago
and what was the issue?
roelof
roelofOP15mo ago
a s too much in the word message
ἔρως
ἔρως15mo ago
yup, you have "messsage" had
roelof
roelofOP15mo ago
🙂
ἔρως
ἔρως15mo ago
don't forget to commit
roelof
roelofOP15mo ago
so now 3 things to do - validation - take care that the css is only loaded when the shortcode is used - sending the data by wp-mail
ἔρως
ἔρως15mo ago
you're not done yet
roelof
roelofOP15mo ago
committed and pushed
ἔρως
ἔρως15mo ago
you still need to make your text translatable ALL OF IT
roelof
roelofOP15mo ago
4 things to do have to think how to do that
ἔρως
ἔρως15mo ago
https://developer.wordpress.org/reference/functions/esc_html__/ you use this function anything that's text, in your form, goes through that function the $domain should be the same prefix you use for everything
roelof
roelofOP15mo ago
so domain should be "mycustomForm" I think
ἔρως
ἔρως15mo ago
yes but you have to use the same domain for EVERYTHING anytime you call that function, you need to use the same domain
roelof
roelofOP15mo ago
oke, then I have to think where in the form I have to use that function
ἔρως
ἔρως15mo ago
anywhere where you have text
roelof
roelofOP15mo ago
also the text that the user has entered in the shortcode ?
ἔρως
ἔρως15mo ago
no
roelof
roelofOP15mo ago
so only the labels and the button at this moment ?
ἔρως
ἔρως15mo ago
that's user provided and should be already translated yup by the way, fix the indentation of your code and don't forget to commit everytime you make a change
roelof
roelofOP15mo ago
yes, boss 😛 it this what you mean
<label for="input" class="control-label">esc_html__("Subject")</label>
<label for="input" class="control-label">esc_html__("Subject")</label>
ἔρως
ἔρως15mo ago
it's a php function actually, you should use esc_html_e it automatically outputs the value for you, so, no echo needed
roelof
roelofOP15mo ago
oke this better :
<label for="input" class="control-label"><?php esc_html_e("Subject") ?></label>

<label for="input" class="control-label"><?php esc_html_e("Subject") ?></label>

ἔρως
ἔρως15mo ago
a lot better now, you're using ids, which is perfectly fine however, there's a bit of a problem: ids are required to be unique
roelof
roelofOP15mo ago
oops and now there are not 😢
ἔρως
ἔρως15mo ago
kinda one way to make unique ids is to use a timestamp and in your css, don't rely on ids
roelof
roelofOP15mo ago
oke, I was thinking about re-using the shortcode-tag I checked real fast and Im not using id 's in my css
ἔρως
ἔρως15mo ago
that works too it's pretty smart
roelof
roelofOP15mo ago
you learned me that trick yesterday on the container thing
ἔρως
ἔρως15mo ago
yup, i remember
roelof
roelofOP15mo ago
after dinner I m looking how to do that
ἔρως
ἔρως15mo ago
it's easier than you might think
roelof
roelofOP15mo ago
I have this :
"<?php echo esc_attr($args['shortcode_tag']) ?>"

"<?php echo esc_attr($args['shortcode_tag']) ?>"

and this :
"for = "input"
"for = "input"
firs idea is to use it like this :
"for = "<?php echo esc_attr($args['shortcode_tag']) ?> . "_input"
"for = "<?php echo esc_attr($args['shortcode_tag']) ?> . "_input"
ἔρως
ἔρως15mo ago
that will almost work
roelof
roelofOP15mo ago
but I hear for me wife that dinner is ready
ἔρως
ἔρως15mo ago
go eat
roelof
roelofOP15mo ago
yes boss 😛
ἔρως
ἔρως15mo ago
lol
roelof
roelofOP15mo ago
one try
"for = "<?php echo esc_attr($args['shortcode_tag']) ?>" . "_input"
"for = "<?php echo esc_attr($args['shortcode_tag']) ?>" . "_input"
ἔρως
ἔρως15mo ago
nope, almost there
roelof
roelofOP15mo ago
😦 and I see that on the input I never use that id so two things to change 😢
ἔρως
ἔρως15mo ago
yup
roelof
roelofOP15mo ago
but now dinner time
ἔρως
ἔρως15mo ago
but there's something you can change after dinner
roelof
roelofOP15mo ago
sorry, I do not see it The only thing I can think of is this :
for = "<?php echo esc_attr($args['shortcode_tag']) ?>" . "_input"
for = "<?php echo esc_attr($args['shortcode_tag']) ?>" . "_input"
`
ἔρως
ἔρως15mo ago
well, you're almost there read carefully on what you're doing there
roelof
roelofOP15mo ago
As far as I see it , I take the for tag open it put the shortcode into it and join it with the text "_input" and close it
ἔρως
ἔρως15mo ago
and where are you closing it?
roelof
roelofOP15mo ago
I thought after the _input text or do I have to use a extra " ?
ἔρως
ἔρως15mo ago
and what's here?
roelof
roelofOP15mo ago
the last character of that string
ἔρως
ἔρως15mo ago
what does the syntax highlight is doing?
roelof
roelofOP15mo ago
never tried
ἔρως
ἔρως15mo ago
try it
roelof
roelofOP15mo ago
first I wanted to be sure that i understand what you mean
ἔρως
ἔρως15mo ago
i don't want to tell you the answer i want to show you the tools to find it yourself write it for just 1 label
roelof
roelofOP15mo ago
I did and the part " _input " is red
ἔρως
ἔρως15mo ago
can you send a screenshot?
roelof
roelofOP15mo ago
and when I try
<label for ="<?php echo esc_attr($args['shortcode_tag']) ?> . _input" class="control-label"><?php esc_html_e("Subject") ?></label>
<label for ="<?php echo esc_attr($args['shortcode_tag']) ?> . _input" class="control-label"><?php esc_html_e("Subject") ?></label>
everything seems to be allright
ἔρως
ἔρως15mo ago
for ="<?php echo esc_attr($args['shortcode_tag']) ?> . _input" <-- is this inside or outside php?
roelof
roelofOP15mo ago
the `. _input" is outside PHP
ἔρως
ἔρως15mo ago
which means, it is html what's the output in the browser?
roelof
roelofOP15mo ago
this ;

for="contact_form . _input"

for="contact_form . _input"
so not good so it seems that it has to be inside PHP
ἔρως
ἔρως15mo ago
or remove the characters you don't want
roelof
roelofOP15mo ago
yes, this gives the rigth answer :
<label for ="<?php echo esc_attr($args['shortcode_tag']) . '_input' ?>" class="control-label"><?php esc_html_e("Subject") ?></label>
<label for ="<?php echo esc_attr($args['shortcode_tag']) . '_input' ?>" class="control-label"><?php esc_html_e("Subject") ?></label>
ἔρως
ἔρως15mo ago
that works too, but it's better to just have it how you had before, but without the .
roelof
roelofOP15mo ago
??
ἔρως
ἔρως15mo ago
for ="<?php echo esc_attr($args['shortcode_tag']) ?>_input"
roelof
roelofOP15mo ago
As far as I know I never tried it without the .
ἔρως
ἔρως15mo ago
try it
roelof
roelofOP15mo ago
works also and it is lot cleaner
ἔρως
ἔρως15mo ago
and it's a tiny tiny tiny tiny tiny tiny tiny tiny tiny tiny tiny bit faster
roelof
roelofOP15mo ago
LOL
ἔρως
ἔρως15mo ago
instead of concatenating a string and then outputting it, you're just outputting everything
roelof
roelofOP15mo ago
now I can do the same with adding the id to the input
ἔρως
ἔρως15mo ago
no not yet
roelof
roelofOP15mo ago
wjich is also forgot 😦
ἔρως
ἔρως15mo ago
esc_attr($args['shortcode_tag']) <-- you will repeat this, so, store it in a variable
roelof
roelofOP15mo ago
so something like :

<?php $shortcode = esc_attr($args['shortcode_tag']) ?>

<?php $shortcode = esc_attr($args['shortcode_tag']) ?>
ἔρως
ἔρως15mo ago
at the top, yes
roelof
roelofOP15mo ago
right after the $atts variable ?
ἔρως
ἔρως15mo ago
yup
roelof
roelofOP15mo ago
Can we also do something like this :
<?php
$atts = array_replace(["class" => "", "subject" => "", "email" => "", "message" => ""], $args['atts']);
$shortcode = esc_attr($args['shortcode_tag'])
?>
<?php
$atts = array_replace(["class" => "", "subject" => "", "email" => "", "message" => ""], $args['atts']);
$shortcode = esc_attr($args['shortcode_tag'])
?>
I find that more clean ?
ἔρως
ἔρως15mo ago
yeah, you SHOULD do that
roelof
roelofOP15mo ago
yes and then the label could be something like this :
<label for ="<?php echo $shortcode ?>_input" class="control-label"><?php esc_html_e("Subject") ?></label>
<label for ="<?php echo $shortcode ?>_input" class="control-label"><?php esc_html_e("Subject") ?></label>
ἔρως
ἔρως15mo ago
yeah, and the id too also, remove the space on the for while you can add spaces, you shouldn't do it
roelof
roelofOP15mo ago
??
ἔρως
ἔρως15mo ago
for =" <-- the space
roelof
roelofOP15mo ago
oke
ἔρως
ἔρως15mo ago
don't forget to commit
roelof
roelofOP15mo ago
so it should be something like this :

<div class="form-group">
<input id="<?php echo $shortcode ?>_input" type="text" required value="<?php echo esc_attr($atts['subject']) ?> " />
<label for="<?php echo $shortcode ?>_input" class="control-label"><?php esc_html_e("Subject") ?></label>
</div>

<div class="form-group">
<input id="<?php echo $shortcode ?>_input" type="text" required value="<?php echo esc_attr($atts['subject']) ?> " />
<label for="<?php echo $shortcode ?>_input" class="control-label"><?php esc_html_e("Subject") ?></label>
</div>
ἔρως
ἔρως15mo ago
yup
roelof
roelofOP15mo ago
pfff I have still a lot to learn
ἔρως
ἔρως15mo ago
way easier than you expected, no?
roelof
roelofOP15mo ago
will change the rest and commit
ἔρως
ἔρως15mo ago
after you commit, you have a bug to fix
roelof
roelofOP15mo ago
it is easy but sometimes I do not see how to fix things like how to make the for unique
ἔρως
ἔρως15mo ago
i know, it comes from practice
roelof
roelofOP15mo ago
and some - one who wants to learn you that things
ἔρως
ἔρως15mo ago
practicing goes a lot further than you imagine
roelof
roelofOP15mo ago
I know I learn the best by doing things instead of just reading things but you said I have a bug 🥲
ἔρως
ἔρως15mo ago
yes, you forgot the domain argument
roelof
roelofOP15mo ago
oke, I have to read back where I have to put it
ἔρως
ἔρως15mo ago
it's the 2nd argument
roelof
roelofOP15mo ago
oke, I have to use it on the esc_html part
ἔρως
ἔρως15mo ago
nope
roelof
roelofOP15mo ago
??? I read back and on that function you talked about domain
ἔρως
ἔρως15mo ago
you should enable the wordpress stubs
roelof
roelofOP15mo ago
and vs code is complaiing on almost all wp functions that it is a unknown function 😦 still confused
ἔρως
ἔρως15mo ago
this
roelof
roelofOP15mo ago
you say to add somethimg
ἔρως
ἔρως15mo ago
intelephense.stubs <-- click on "add item" then add wordpress
roelof
roelofOP15mo ago
I did but stiill this :
No description
ἔρως
ἔρως15mo ago
restart vscode
roelof
roelofOP15mo ago
I already did funny it does not know that function but when I hover over it , it gives back that I need a domain looks to me now that I need a domain on the esc_html_e part so commited and pushed
ἔρως
ἔρως15mo ago
yup, that's where you need the domain
roelof
roelofOP15mo ago
I thought or I mean I said that earlier and then you said no
ἔρως
ἔρως15mo ago
you're right, i misread it
roelof
roelofOP15mo ago
What is next on the big list ? oke i was confused but everyone can make mistakes
ἔρως
ἔρως15mo ago
have you tried to run your code?
roelof
roelofOP15mo ago
yep and it seems to run fine
ἔρως
ἔρως15mo ago
nice
roelof
roelofOP15mo ago
except when I press the submit it reloads the page and that is normal
ἔρως
ἔρως15mo ago
commit and push
roelof
roelofOP15mo ago
and I miss a line under the input , have to look good with css where that one is gone I did already 🙂
ἔρως
ἔρως15mo ago
one thing you can try, which goes against wordpress' guidelines, is the short echo syntax you can use <?= ... ?> instead of <?php echo ... ?>
roelof
roelofOP15mo ago
that is oke this one is for a challenge and is never meant to use in real
ἔρως
ἔρως15mo ago
alright, then use that and fix your indentation
roelof
roelofOP15mo ago
done and done
ἔρως
ἔρως15mo ago
good i will insert some bias here: use tabs instead of spaces, for indentation
roelof
roelofOP15mo ago
oke then I have to change some setting in PHP
ἔρως
ἔρως15mo ago
in php?
roelof
roelofOP15mo ago
no in vs code sorry also changed
roelof
roelofOP15mo ago
aha I thought I did handle the css file here :
function load_assets()
{
wp_enqueue_style(
'mycustomForm',
plugin_dir_url(__FILE__) . '/css/mycustomForm.css',
array(),
1,
'all'
);
}

add_action('wp_enqueue_scripts', 'load_assets');
function load_assets()
{
wp_enqueue_style(
'mycustomForm',
plugin_dir_url(__FILE__) . '/css/mycustomForm.css',
array(),
1,
'all'
);
}

add_action('wp_enqueue_scripts', 'load_assets');
if you do not mind, I take a shower . Be back in 30 min
ἔρως
ἔρως15mo ago
take your time
roelof
roelofOP15mo ago
I have read that page but confused what now the right way is
ἔρως
ἔρως15mo ago
it's fine, i will help you in a bit
roelof
roelofOP15mo ago
The author of the challenge says use has_shortcode but that page said there are better ways
ἔρως
ἔρως15mo ago
but i have a much better idea
roelof
roelofOP15mo ago
BRB in about 30 min
ἔρως
ἔρως15mo ago
alright
roelof
roelofOP15mo ago
and im back So you wanted to learn me a better way to take care that the css is onlky loaded when the shortcode is used
ἔρως
ἔρως15mo ago
yes instead of enqueing the css, just register it
roelof
roelofOP15mo ago
oke
ἔρως
ἔρως15mo ago
then, inside roelof_contact_form, you can enqueue the css
roelof
roelofOP15mo ago
is that explained in the page you linked to
ἔρως
ἔρως15mo ago
not really, not exactly
roelof
roelofOP15mo ago
oke, I do not know what to think of that is a way I never saw or heared from
ἔρως
ἔρως15mo ago
it's kinda like how the post shows, but you only enqueue once you can add a static variable to keep track of if it is the first time you run the function
roelof
roelofOP15mo ago
oke can we do that step by step I have now this :
function load_assets()
{
wp_enqueue_style(
'mycustomForm',
plugin_dir_url(__FILE__) . '/css/mycustomForm.css',
array(),
1,
'all'
);
}

add_action('wp_enqueue_scripts', 'load_assets');
function load_assets()
{
wp_enqueue_style(
'mycustomForm',
plugin_dir_url(__FILE__) . '/css/mycustomForm.css',
array(),
1,
'all'
);
}

add_action('wp_enqueue_scripts', 'load_assets');
you mean something like :
function load_assets()
{

wp_register_style( 'get-style', plugins_url( '/css/mycustomForm.css', __FILE__ ), array(), '1.0.0', 'all' );
}
add_action('wp_enqueue_scripts', 'load_assets');
function load_assets()
{

wp_register_style( 'get-style', plugins_url( '/css/mycustomForm.css', __FILE__ ), array(), '1.0.0', 'all' );
}
add_action('wp_enqueue_scripts', 'load_assets');
and then in the form do this :
<?php wp_enqueue_style( 'get-style' ); ?>
<?php wp_enqueue_style( 'get-style' ); ?>
That seems to work I can find the css in the page where I use the shortcode and I cannot find the css in a page where the shortcode is not used Another point solved What is there still on the list ? latest code is now on gitlab 🙂 I will look at this but goto bed in a hour Tomorrow time to work on my first template at "work"
ἔρως
ἔρως15mo ago
sorry, got distracted with overwatch
roelof
roelofOP15mo ago
not a problem
ἔρως
ἔρως15mo ago
get-style <-- this isn't unique enough
roelof
roelofOP15mo ago
I have the rest of the month to finish this challenge and then a new one gets send oke is get-rw-custom-form-style better ?
ἔρως
ἔρως15mo ago
yes, that is better but remove the get
roelof
roelofOP15mo ago
oke changed committed and pushed tomorrow work on the next points ?
ἔρως
ἔρως15mo ago
just one thing wp_enqueue_script( 'my-script'); <-- you didn't use this i copied it from the post but you didn't enqueue the css, only registered it
roelof
roelofOP15mo ago
correct, I did not know if we are going to use js for client validation
ἔρως
ἔρως15mo ago
we can, but that's for later are you sure the css works?
roelof
roelofOP15mo ago
yes I did this in form.php
ἔρως
ἔρως15mo ago
no, don't do it there
roelof
roelofOP15mo ago
wp_enqueue_style( 'rw-custom-form-style' );
wp_enqueue_style( 'rw-custom-form-style' );
ἔρως
ἔρως15mo ago
do it in the function that calls the form
roelof
roelofOP15mo ago
o, I understand that I had to do that
ἔρως
ἔρως15mo ago
no, no it's in the function
roelof
roelofOP15mo ago
this one :
function roelof_contact_form($atts, $content, $shortcode_tag)
{
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode_tag' => $shortcode_tag,
]);
return ob_end_flush();
}
function roelof_contact_form($atts, $content, $shortcode_tag)
{
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode_tag' => $shortcode_tag,
]);
return ob_end_flush();
}
ἔρως
ἔρως15mo ago
yup
roelof
roelofOP15mo ago
have to think then where I must put the code then
ἔρως
ἔρως15mo ago
on top
roelof
roelofOP15mo ago
before the ob_start() ?
ἔρως
ἔρως15mo ago
yes
roelof
roelofOP15mo ago
still works
ἔρως
ἔρως15mo ago
what happens when you use it twice?
roelof
roelofOP15mo ago
Tommorow work on the client or the server validation ? or is there another thing to do
ἔρως
ἔρως15mo ago
tomorrow, it's submitting and validating
roelof
roelofOP15mo ago
css is one time loaded and is still working
ἔρως
ἔρως15mo ago
nice then
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
and the ids are duplicated?
roelof
roelofOP15mo ago
I knpw that is it submitting and validating But as far as I know yiou can do server and client validation
ἔρως
ἔρως15mo ago
no no, you MUST do server-side validation
roelof
roelofOP15mo ago
chips, the ids are the same on both forms
ἔρως
ἔρως15mo ago
it's mandatory, not optional you can do something simple
roelof
roelofOP15mo ago
So work to do how to solve that ;:'(
ἔρως
ἔρως15mo ago
add a static variable with a count
roelof
roelofOP15mo ago
I know , The most/best way as far as I know is first client validation and if that is successfull do server validation and then send it with wp_mail if the server validation is also successfull oke, I will think about how to do that tomorrow Right now very tired Thanks for the lessons
ἔρως
ἔρως15mo ago
alright, go rest you're welcome
roelof
roelofOP15mo ago
I hope im a good student and not a F -one if you live in the USA
ἔρως
ἔρως15mo ago
you've been doing well, so far you mean, asshole?
roelof
roelofOP15mo ago
I mean a F student like in fail
ἔρως
ἔρως15mo ago
you said A
roelof
roelofOP15mo ago
Here in the netherlands we give grades from 1 - 10 Where 1 is very very bad and 10 excellent
ἔρως
ἔρως15mo ago
you edited it 🤣
roelof
roelofOP15mo ago
Yep I go then for a 6 GN
ἔρως
ἔρως15mo ago
im not from usa, we use the same scale here goodnight
roelof
roelofOP15mo ago
GN and like I said i go for a 6 or 7
ἔρως
ἔρως15mo ago
i think a 7 is fair
roelof
roelofOP15mo ago
where do you live then ? I live in the Netherlands
ἔρως
ἔρως15mo ago
portugal
roelof
roelofOP15mo ago
nice country I was there some 30 years ago in a small city called Felguras If I write it well it was a small city near Porto I had there a pen friend
ἔρως
ἔρως15mo ago
you're close, it's felgueiras i had to google that, just to be sure
roelof
roelofOP15mo ago
oke Csn I try to make a client validation in plain js tomorrow After I have figured out that counter part ? and now realy going to sleep
ἔρως
ἔρως15mo ago
you can, yes
roelof
roelofOP15mo ago
oke, first find out how to work with the counter this afternoon This morning time for work I wonder if I can add the counter variable here :
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode_tag' => $shortcode_tag,
]);
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode_tag' => $shortcode_tag,
]);
like this :
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode_tag' => $shortcode_tag,
'counter' => $counter || 0 ;
]);
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode_tag' => $shortcode_tag,
'counter' => $counter || 0 ;
]);
` I do not think so because the variable counter is then still unknown second idea was to add this in form.php
$counter = $counter + 1 || 0 ;
$counter = $counter + 1 || 0 ;
but then counter is on both 1 so back to the drawing board this afternoon
ἔρως
ἔρως15mo ago
you are mixing javascript and php, and even that is wrong in javascript do this in the function that renders the shortcode then you can add this there are some improvements to be done today, but thats for later
roelof
roelofOP15mo ago
that was my first idea to use this one :
function roelof_add_custom_shortcode()
{
function roelof_contact_form($atts, $content, $shortcode_tag)
{
wp_enqueue_style( 'rw-custom-form-style' );
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode_tag' => $shortcode_tag,
]);
return ob_end_flush();
}

add_shortcode('contact_form', 'roelof_contact_form');
}
function roelof_add_custom_shortcode()
{
function roelof_contact_form($atts, $content, $shortcode_tag)
{
wp_enqueue_style( 'rw-custom-form-style' );
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode_tag' => $shortcode_tag,
]);
return ob_end_flush();
}

add_shortcode('contact_form', 'roelof_contact_form');
}
but I do not see how I can make it work that counter is 0 except when it has already a value I hope your boss will not be angry because you help me now
ἔρως
ἔρως15mo ago
i dont have schedules, and work from home
roelof
roelofOP15mo ago
Im still thinking in the load_templates part looks the most logical area because I have to use the value in the template file
ἔρως
ἔρως15mo ago
no, above the enqueued style thats where you add the static variable always declare global and static variables at the top
roelof
roelofOP15mo ago
oke, so it willl be something like this :
function roelof_add_custom_shortcode()
{
function roelof_contact_form($atts, $content, $shortcode_tag)
{
if (isset($counter) {
$counter = counter + 1
} else {
$counter = 1
}
wp_enqueue_style( 'rw-custom-form-style' );
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode_tag' => $shortcode_tag,
]);
return ob_end_flush();
}

add_shortcode('contact_form', 'roelof_contact_form');
}
function roelof_add_custom_shortcode()
{
function roelof_contact_form($atts, $content, $shortcode_tag)
{
if (isset($counter) {
$counter = counter + 1
} else {
$counter = 1
}
wp_enqueue_style( 'rw-custom-form-style' );
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode_tag' => $shortcode_tag,
]);
return ob_end_flush();
}

add_shortcode('contact_form', 'roelof_contact_form');
}
something like this ? but is counter then known in the form.php file ?
ἔρως
ἔρως15mo ago
no, just a static variable that code wont work everytime you run the shortcode, it will start from 0
roelof
roelofOP15mo ago
oke did some google and found this :

function roelof_add_custom_shortcode()
{
function roelof_contact_form($atts, $content, $shortcode_tag)
{
static $counter = 0;
$counter ++ ;
wp_enqueue_style( 'rw-custom-form-style' );
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode_tag' => $shortcode_tag,
]);
return ob_end_flush();
}

add_shortcode('contact_form', 'roelof_contact_form');
}

function roelof_add_custom_shortcode()
{
function roelof_contact_form($atts, $content, $shortcode_tag)
{
static $counter = 0;
$counter ++ ;
wp_enqueue_style( 'rw-custom-form-style' );
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode_tag' => $shortcode_tag,
]);
return ob_end_flush();
}

add_shortcode('contact_form', 'roelof_contact_form');
}
This better ?
ἔρως
ἔρως15mo ago
yes, but now, you need to pass the counter to the theme template but, i would do something better i would pass a variable called "prefix", which has the name of the shortcode and the counter appended then where you used the shortcode variable for the names, replace with the prefix variable
roelof
roelofOP15mo ago
I hope I understand you well here :
function roelof_add_custom_shortcode()
{
function roelof_contact_form($atts, $content, $shortcode_tag)
{
static $counter = 0;
$counter ++ ;
wp_enqueue_style( 'rw-custom-form-style' );
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'prefix' => $shortcode_tag . $counter,
]);
return ob_end_flush();
}

add_shortcode('contact_form', 'roelof_contact_form');
}
function roelof_add_custom_shortcode()
{
function roelof_contact_form($atts, $content, $shortcode_tag)
{
static $counter = 0;
$counter ++ ;
wp_enqueue_style( 'rw-custom-form-style' );
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'prefix' => $shortcode_tag . $counter,
]);
return ob_end_flush();
}

add_shortcode('contact_form', 'roelof_contact_form');
}
that seems to work but it also breaks my css totallt it breaks on this line :
<div class="<?= esc_attr($atts['class']) ?>" data-shortcode="<?= esc_attr($args['prefix']) ?>">
<div class="<?= esc_attr($atts['class']) ?>" data-shortcode="<?= esc_attr($args['prefix']) ?>">
which now says contact_form1 and contact_form2 Where the css is looking at
[data-shortcode="contact_form"] {
[data-shortcode="contact_form"] {
This why I still doubt if developing is for me try to solve one problem and run into multiple more
ἔρως
ἔρως15mo ago
thats because you changed on the data-shortcode, but i said the name (i meant id and "for")
roelof
roelofOP15mo ago
oke, I had to because the shortcode tag does not exist or shouldi make a prefix and shortcode part as argument as earlier I have problems to see what you exactly wants from me
ἔρως
ἔρως15mo ago
that one should stay as it is the rest, you can use the prefix variable
roelof
roelofOP15mo ago
oke so like this :
function roelof_add_custom_shortcode()
{
function roelof_contact_form($atts, $content, $shortcode_tag)
{
static $counter = 0;
$counter ++ ;
wp_enqueue_style( 'rw-custom-form-style' );
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode' => $shortcode_tag,
'prefix' => $shortcode_tag . $counter,
]);
return ob_end_flush();
}

add_shortcode('contact_form', 'roelof_contact_form');
}

add_action('init', 'roelof_add_custom_shortcode');
function roelof_add_custom_shortcode()
{
function roelof_contact_form($atts, $content, $shortcode_tag)
{
static $counter = 0;
$counter ++ ;
wp_enqueue_style( 'rw-custom-form-style' );
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode' => $shortcode_tag,
'prefix' => $shortcode_tag . $counter,
]);
return ob_end_flush();
}

add_shortcode('contact_form', 'roelof_contact_form');
}

add_action('init', 'roelof_add_custom_shortcode');
` Then it works fine so now writing the js for the client validation so also add a call to the js file the same way as did with the css will costs me I think a few days
ἔρως
ἔρως15mo ago
it will be easier than you think but yes, the javascript file should be included the same way
roelof
roelofOP15mo ago
I know I did thiis earlier client validation but also I have nog much energy because I had corona and stilll have some mental problems so I cannot be coding for long on a day
ἔρως
ἔρως15mo ago
its fine, i will be off available in 5-6 hours
roelof
roelofOP15mo ago
oke And give it uo for today Done this :
var button = document.querySelector('button');

button.addEventListener('click', (e) => {
e.preventDefault();

// make a array for all the error messages.
var error_messages = [];

//take the value of the subject
var subject = document.querySelector('.subject').value ;


//validate the subject so it cannot be empty

if (subject.length < 3) {
error_messages.push ("Subject must be greater then 3 characters");
console.log('found a error');
}


// show the error messages in the error area

var error_area = document.querySelector('.error_messages');

var errors = document.createElement('div');
errors.classList.add('have_errors');

error_messages.forEach( (message) => {
errors.innerhtml = message
})

error_area.appendChild(errors);



})
var button = document.querySelector('button');

button.addEventListener('click', (e) => {
e.preventDefault();

// make a array for all the error messages.
var error_messages = [];

//take the value of the subject
var subject = document.querySelector('.subject').value ;


//validate the subject so it cannot be empty

if (subject.length < 3) {
error_messages.push ("Subject must be greater then 3 characters");
console.log('found a error');
}


// show the error messages in the error area

var error_area = document.querySelector('.error_messages');

var errors = document.createElement('div');
errors.classList.add('have_errors');

error_messages.forEach( (message) => {
errors.innerhtml = message
})

error_area.appendChild(errors);



})
but I do not see the error messaages so I do something wrong and I do not have the energy to find out why be back online maybe if my daugther is too bed after 20:00 hours
ἔρως
ἔρως15mo ago
you have a terrible mistake there you should only handle the submit of the form, not a click on buttons
roelof
roelofOP15mo ago
??? is that not the same by pressing the button I want to submit I do it all the time with js
ἔρως
ἔρως15mo ago
no, its not one is a click on a button the other is a form that is being submitted you can submit a form in many different ways
roelof
roelofOP15mo ago
chips, then some body has learned me very very wrong things in js
ἔρως
ἔρως15mo ago
for example, the enter key or the send key on some virtual keyboards
roelof
roelofOP15mo ago
oke, So I have to find the form and make a eventlistener on submit ?
ἔρως
ἔρως15mo ago
yup and guess what? all forms have a common data attribute so, its not as hard as you may think
roelof
roelofOP15mo ago
oke, I changed the code to this :
var form = document.querySelector('.form')

form.addEventListener('submit', (e) => {

// make a array for all the error messages.
var error_messages = [];

//take the value of the subject
var subject = document.querySelector('.subject').value ;


//validate the subject so it cannot be empty

if (subject.length < 3) {
error_messages.push ("Subject must be greater then 3 characters");
}


// show the error messages in the error area

var error_area = document.querySelector('.error_messages');

var errors = document.createElement('div');
errors.classList.add('have_errors');

error_messages.forEach( (message) => {
errors.innerhtml = message
})

error_area.appendChild(errors);



})
var form = document.querySelector('.form')

form.addEventListener('submit', (e) => {

// make a array for all the error messages.
var error_messages = [];

//take the value of the subject
var subject = document.querySelector('.subject').value ;


//validate the subject so it cannot be empty

if (subject.length < 3) {
error_messages.push ("Subject must be greater then 3 characters");
}


// show the error messages in the error area

var error_area = document.querySelector('.error_messages');

var errors = document.createElement('div');
errors.classList.add('have_errors');

error_messages.forEach( (message) => {
errors.innerhtml = message
})

error_area.appendChild(errors);



})
ἔρως
ἔρως15mo ago
that wont work because you are selecting any form, and not the ones you want
roelof
roelofOP15mo ago
I call it a day and maybe leave devloping at all My "mentors" has learned me the last 3 years more bad things then good things I see more and more
ἔρως
ἔρως15mo ago
how about you take a break? im working now, and you need to rest your head
roelof
roelofOP15mo ago
I will do that in a hpur I have to bring my daugther to sport and having dinner So now computer till about 20:00 hour (Amsterdam time) At the moment no idea how I can take care that the right one is choosen
ἔρως
ἔρως15mo ago
then focus on her for that time
roelof
roelofOP15mo ago
I will do that but I think the last 2 - 3 days you saw why im always get stuck at this sort of stuff
ἔρως
ἔρως15mo ago
yes, but you also lack practice which is fine
roelof
roelofOP15mo ago
The onlu way I can be in the right form is getting the id and as far a I know I cannot get that without the button push I think and then we could fetch the subnmitting
ἔρως
ἔρως15mo ago
you can get all the elements with the data- attribute, then select the forms that are children
roelof
roelofOP15mo ago
oke, but how do I then know which form is submitted
ἔρως
ἔρως15mo ago
you dont need to the this or event.target will have the form
roelof
roelofOP15mo ago
I do not But that can mean that on a wrong form some validation errors are shown
ἔρως
ἔρως15mo ago
nope
roelof
roelofOP15mo ago
aha, so I know the form already
ἔρως
ἔρως15mo ago
exactly you dont need to find the form, it will be given to you
roelof
roelofOP15mo ago
oke, back to experimenting to see this in real you have to totally other way of thinking that I learned
ἔρως
ἔρως15mo ago
after you implement that, i have something for you to add
roelof
roelofOP15mo ago
but that is oke so tomorrow 1) find all forms
2) find out what e.target contains
ἔρως
ἔρως15mo ago
today, you will add a reset button, with a few lines of php, css and 1 line of html
roelof
roelofOP15mo ago
hmm, for e.target to be found there has to be a event ?
ἔρως
ἔρως15mo ago
yes, the form submit
roelof
roelofOP15mo ago
oke, I will try the reset button this evening or tommorow
ἔρως
ἔρως15mo ago
good, we can try it later
roelof
roelofOP15mo ago
?? now im confused because for the submit I need the form but for the form I need a event Now really time for a break in 20 min I have to go
ἔρως
ἔρως15mo ago
for the form to work, you need 1 thing, a button of type submit thats it
roelof
roelofOP15mo ago
and I have that I did that in my old code and then you said that is wrong
ἔρως
ἔρως15mo ago
yes, because you added a click event to the button what if the form is submitted by pressing enter? or the send key on android?
roelof
roelofOP15mo ago
pffff
ἔρως
ἔρως15mo ago
that is why you change the code to listen to a submit event on the form, instead of a click on a button
roelof
roelofOP15mo ago
yep
ἔρως
ἔρως15mo ago
makes sense?
roelof
roelofOP15mo ago
I did then this :
form.addEventListener('submit', () => {
form.addEventListener('submit', () => {
but that was also wrong Can we talk tomorrow or maybe this evening for a very short time
ἔρως
ἔρως15mo ago
yes, because form is a nodelist, not a form sure, we can
roelof
roelofOP15mo ago
Yep when we use queryselectorall we get anode list So now make a evenlistener on all form with as event the submit? I have to wait a hour now I have this :
var all_forms = document.querySelectorAll('[data-attribute="contact_form"]');

all_forms.forEach ( (form) => {
form.addeventlistener('submit' , (e) => {
e.preventDefault();
console.log(e)
})
})
var all_forms = document.querySelectorAll('[data-attribute="contact_form"]');

all_forms.forEach ( (form) => {
form.addeventlistener('submit' , (e) => {
e.preventDefault();
console.log(e)
})
})
but I cannot see e because the browser reloads all the time Is there a trick this can work
ἔρως
ἔρως15mo ago
document.querySelectorAll('[data-attribute="contact_form"]') <-- this should be the data attribute you defined for the shortcode then, you can select the form addeventlistener <-- this doesn't exist addEventListener <-- this is the correct spelling
roelof
roelofOP15mo ago
I did that as far as I know
<div class="" data-shortcode="contact_form">
<div class="" data-shortcode="contact_form">
ἔρως
ἔρως15mo ago
compare with what's there
roelof
roelofOP15mo ago
sorry, but for me it is the same or do you mean data-shortcode against data-atribute
ἔρως
ἔρως15mo ago
yes if it isn't exactly the same, then it won't match and you will never find any forms or divs or anything
roelof
roelofOP15mo ago
yep, now I can see what e exactly is and think about the next step
ἔρως
ἔρως15mo ago
and what's the next step?
roelof
roelofOP15mo ago
Find out which of the two forms im using or are submnitting
ἔρως
ἔρως15mo ago
e.target
roelof
roelofOP15mo ago
target is not working because it is empty 😢
ἔρως
ἔρως15mo ago
if it is empty, something is wrong
roelof
roelofOP15mo ago
it is empty right now
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
you're on "i" target will be after
roelof
roelofOP15mo ago
I use now this js code :
var all_forms = document.querySelectorAll('[data-shortcode="contact_form"]');

all_forms.forEach ( (form) => {
form.addEventListener('submit' , (e) => {
e.preventDefault();
console.log(e)
})
})
var all_forms = document.querySelectorAll('[data-shortcode="contact_form"]');

all_forms.forEach ( (form) => {
form.addEventListener('submit' , (e) => {
e.preventDefault();
console.log(e)
})
})
ἔρως
ἔρως15mo ago
just try to type e.target instead of e
roelof
roelofOP15mo ago
oke I see then this :
roelof
roelofOP15mo ago
No description
roelof
roelofOP15mo ago
so still not the id of something I can indentify the form
ἔρως
ἔρως15mo ago
you don't need one
roelof
roelofOP15mo ago
oke, so then the next step would be to find out what value subject has ?
ἔρως
ἔρως15mo ago
no what you will do is super simple
roelof
roelofOP15mo ago
oke, still confused how you want me to handle this
ἔρως
ἔρως15mo ago
make sure the email field has the email type make sure all required fields have the required attribute
roelof
roelofOP15mo ago
o, I thought I had to check if subject is for example not empty and that email is a valid email and so on
ἔρως
ἔρως15mo ago
you can do that do you want to implement it from scratch?
roelof
roelofOP15mo ago
that was my idea
ἔρως
ἔρως15mo ago
alright, well, then it will be a bit harder, but it's fine
roelof
roelofOP15mo ago
that is why I made a check if subject has a length of more then 2 my 2 cts are that validation takes care that a userr cannot enter things we do not want like a empty subject or so or a very long subject
ἔρως
ἔρως15mo ago
that's something you can implement first, you need to implement an element that will display an error message
roelof
roelofOP15mo ago
I have already placed this :
<div class="user_feedback">

</div>
<div class="user_feedback">

</div>
above the form tag as the requirements wanted me to do
ἔρως
ἔρως15mo ago
on top of the form? and not on each element?
roelof
roelofOP15mo ago
yep pity that is what the challenge wanted
ἔρως
ἔρως15mo ago
then you should check all fields, and store all errors in an array then, show all the errors after
roelof
roelofOP15mo ago
Display errors on top of the form
Display errors on top of the form
ἔρως
ἔρως15mo ago
if the array with errors is empty, then just let it submit or you submit via ajax
roelof
roelofOP15mo ago
that is also my idea and it was in the code I posted earlier so still I think I have to find a way to see what is filled in the subject field of the form that is submitted and thinking how to do if I know only the whole form with e.target
ἔρως
ἔρως15mo ago
there's 2 ways: 1- form.<field_name> 2- form.querySelector('[name="field_name"]')
roelof
roelofOP15mo ago
for the submit I also need your help
ἔρως
ἔρως15mo ago
you don't need anything else besides a form
roelof
roelofOP15mo ago
so something like this :
var all_forms = document.querySelectorAll('[data-shortcode="contact_form"]');

all_forms.forEach ( (form) => {
form.addEventListener('submit' , (e) => {
e.preventDefault();

// array that holds all the validation errors

var error_messages = [] ;

// find the subject field of the form

var subject = form.querySelector('subject').value

if (subject.length < 3) {
error_messages.push('Subject has to be more then 3 characters');
}




})
})
var all_forms = document.querySelectorAll('[data-shortcode="contact_form"]');

all_forms.forEach ( (form) => {
form.addEventListener('submit' , (e) => {
e.preventDefault();

// array that holds all the validation errors

var error_messages = [] ;

// find the subject field of the form

var subject = form.querySelector('subject').value

if (subject.length < 3) {
error_messages.push('Subject has to be more then 3 characters');
}




})
})
ἔρως
ἔρως15mo ago
that wont work there's no <subject> element
roelof
roelofOP15mo ago
then tomorrow I have to find out how I can display the messages on the right places
ἔρως
ἔρως15mo ago
also, instead of your comments, use jsdoc and in php, use phpdoc but that's for last
roelof
roelofOP15mo ago
sorry I mean this :
var subject = form.querySelector('.subject').value
var subject = form.querySelector('.subject').value
ἔρως
ἔρως15mo ago
did you add a class to the field?
roelof
roelofOP15mo ago
never used jsdoc or phpdoc so no idea how those work yep I added that class
ἔρως
ἔρως15mo ago
why did you add the class?
roelof
roelofOP15mo ago
because I learned to do it like that on my "work" to heal from mental problems so for every part I need to add :
/**
* Represents a book.
* @constructor
* @param {string} title - The title of the book.
* @param {string} author - The author of the book.
*/
/**
* Represents a book.
* @constructor
* @param {string} title - The title of the book.
* @param {string} author - The author of the book.
*/
ἔρως
ἔρως15mo ago
do you use the class in css?
roelof
roelofOP15mo ago
I do not use that class in css
ἔρως
ἔρως15mo ago
that's sorta the idea then the class is useless, and you can target the element by the name
roelof
roelofOP15mo ago
then I have to add a name right now the input does not have a name
<input id="<?= $prefix ?>_input" type="text" required value="<?= esc_attr($atts['subject']) ?> " />
<input id="<?= $prefix ?>_input" type="text" required value="<?= esc_attr($atts['subject']) ?> " />
ἔρως
ἔρως15mo ago
a name is a requirement all fields must have a name
roelof
roelofOP15mo ago
oke, then I have to think well on a name
ἔρως
ἔρως15mo ago
and the id in that input should end in _subject keep the ids descriptive what's that field?
roelof
roelofOP15mo ago
it is the subject
ἔρως
ἔρως15mo ago
then why don't you call it subject?
roelof
roelofOP15mo ago
and I thougth the id was well yesterday
ἔρως
ἔρως15mo ago
it's not descriptive enough i didn't notice it
roelof
roelofOP15mo ago
this better :
<input id="<?= $prefix ?>_input_subject" name="subject" type="text" required value="<?= esc_attr($atts['subject']) ?> " />
<input id="<?= $prefix ?>_input_subject" name="subject" type="text" required value="<?= esc_attr($atts['subject']) ?> " />
ἔρως
ἔρως15mo ago
no, just _subject
roelof
roelofOP15mo ago
then I have to dive into js how querySelectors work with the name field
ἔρως
ἔρως15mo ago
input is redundant and only fills space it's a css selector
roelof
roelofOP15mo ago
so something like this better :
var subject = form.querySelector('[input="name"]').value
var subject = form.querySelector('[input="name"]').value
ἔρως
ἔρως15mo ago
it is better but, you can also do form.subject
roelof
roelofOP15mo ago
nope when I do :
var subject = form.querySelector(form.subject).value;
var subject = form.querySelector(form.subject).value;
I see this :
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
that's because it's just form.subject console.log that
roelof
roelofOP15mo ago
var subject = form.querySelector(form.subject)
var subject = form.querySelector(form.subject)
gives a message that subject is null
ἔρως
ἔρως15mo ago
no, not that read this carefully
roelof
roelofOP15mo ago
This is my html
<div class="form-group">
<input id="<?= $prefix ?>_subject" name="subject" type="text" required value="<?= esc_attr($atts['subject']) ?> " />
<label for="<?= $prefix ?>_subject" class="control-label"><?php esc_html_e("Subject", "mycustomForm") ?></label>
</div>

<div class="form-group">
<input id="<?= $prefix ?>_subject" name="subject" type="text" required value="<?= esc_attr($atts['subject']) ?> " />
<label for="<?= $prefix ?>_subject" class="control-label"><?php esc_html_e("Subject", "mycustomForm") ?></label>
</div>

still very confused this gives the same errror message
var subject = form.subject
var subject = form.subject
So I think we misunderstood each other now
ἔρως
ἔρως15mo ago
and what's in the form?
roelof
roelofOP15mo ago
<div class="form-group">
<input id="<?= $prefix ?>_subject" name="subject" type="text" required value="<?= esc_attr($atts['subject']) ?> " />
<label for="<?= $prefix ?>_subject" class="control-label"><?php esc_html_e("Subject", "mycustomForm") ?></label>
</div>
<div class="form-group">
<input id="<?= $prefix ?>_subject" name="subject" type="text" required value="<?= esc_attr($atts['subject']) ?> " />
<label for="<?= $prefix ?>_subject" class="control-label"><?php esc_html_e("Subject", "mycustomForm") ?></label>
</div>
ἔρως
ἔρως15mo ago
in the form variable
roelof
roelofOP15mo ago
a lot of things but no subject or whatever name
ἔρως
ἔρως15mo ago
show me the console log
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
that's not a form that's a div
roelof
roelofOP15mo ago
pfff Then maybe this is still wrong ?

var all_forms = document.querySelectorAll('[data-shortcode="contact_form"]');

var all_forms = document.querySelectorAll('[data-shortcode="contact_form"]');
ἔρως
ἔρως15mo ago
you have to select the form inside the div
roelof
roelofOP15mo ago
??
ἔρως
ἔρως15mo ago
it's not wrong, just incomplete
roelof
roelofOP15mo ago
pfff, I call it a day \ I m more and more confused
ἔρως
ἔρως15mo ago
i know, javascript makes it easy to get lost
roelof
roelofOP15mo ago
you are a good teacher but it is sometimes hard for me what you exacrtly wants must i place another querySelector to find the form
ἔρως
ἔρως15mo ago
no think about css selectors how would you style the form inside that div?
roelof
roelofOP15mo ago
normally I would use the form tag
ἔρως
ἔρως15mo ago
but that, alone, selects all forms, which is wrong
roelof
roelofOP15mo ago
but here I make a div around it which I use [data-shortcode] in the css
ἔρως
ἔρως15mo ago
that's correct so, how do you select the form inside it?
roelof
roelofOP15mo ago
I would do form.querySelector('form')
ἔρως
ἔρως15mo ago
no that's not css, that's javascript think only about css
roelof
roelofOP15mo ago
oke
ἔρως
ἔρως15mo ago
you're using css selectors to select elements, right?
roelof
roelofOP15mo ago
then I would do
[data-shortcode="contact_form"] > form
[data-shortcode="contact_form"] > form
ἔρως
ἔρως15mo ago
exactly so, use that in the javascript
roelof
roelofOP15mo ago
oke, you mean like this :
var all_forms = document.querySelectorAll('[data-shortcode="contact_form"] > form');
var all_forms = document.querySelectorAll('[data-shortcode="contact_form"] > form');
ἔρως
ἔρως15mo ago
yup when you stop and think about it, you can "un-confuse" yourself
roelof
roelofOP15mo ago
oke
roelof
roelofOP15mo ago
I have now this :
No description
ἔρως
ἔρως15mo ago
and now, the code works? do you use the .form class in your css?
roelof
roelofOP15mo ago
nope
ἔρως
ἔρως15mo ago
remove the class
roelof
roelofOP15mo ago
done
ἔρως
ἔρως15mo ago
your form variable should have the correct form now
roelof
roelofOP15mo ago
yeo form.subject gives now this :
<input id="contact_form1_subject" name="subject" type="text" required="" value="m ">
<input id="contact_form1_subject" name="subject" type="text" required="" value="m ">
ἔρως
ἔρως15mo ago
nice, now you should have easy access to all elements it isn't the best idea, but it works and it's easier for you
roelof
roelofOP15mo ago
yep I think I can do now form.subject.value
ἔρως
ἔρως15mo ago
yup, you can
roelof
roelofOP15mo ago
oke, time to sleep and tomorow think how I can show the errors in the feedback div GN
ἔρως
ἔρως15mo ago
just push errors into the variable
roelof
roelofOP15mo ago
and thanks for the lessons
ἔρως
ἔρως15mo ago
goodnight
roelof
roelofOP15mo ago
I did
error_messages.push('Subject has to be more then 3 characters');
error_messages.push('Subject has to be more then 3 characters');
ἔρως
ἔρως15mo ago
nice
roelof
roelofOP15mo ago
first I want to be sure that I can show that message before I can check if the email and so on is valid that is a easy one I think
ἔρως
ἔρως15mo ago
i think that that's a good idea
roelof
roelofOP15mo ago
oke I see now the validation errrors . If the javascript is allright , I will make the css so you can really see the messages. UPdated the repo and I hope you are happy Next thing will be figure out how I can check if a email is right I think I need some sort of regex for that L;( hmm, still some sort of error on the second form there is no error found I assume that querySelector find the first one but not the second
ἔρως
ἔρως15mo ago
querySelector only selects 1 element well, you can use a basic regex to verify the email, and then do a better test in the server
roelof
roelofOP15mo ago
I know, but when I do QuerySelectorAll I got them all But then I have to figure out which one
ἔρως
ἔρως15mo ago
what are you trying to do?
roelof
roelofOP15mo ago
I try to display the validation errors on the right form I cannot work for 2 hours because I get proffesional help for my problems
ἔρως
ἔρως15mo ago
you just need to select the right div and throw all errors there then preferably in a list
roelof
roelofOP15mo ago
oke, I know use a p tag
ἔρως
ἔρως15mo ago
i wouldnt use a paragraph, but a list
roelof
roelofOP15mo ago
oke, first I wanted to see how to solve the prpblem that the error message is shown in the right form Do you have any hints ?
ἔρως
ἔρως15mo ago
yes: everything you need is inside the form
roelof
roelofOP15mo ago
oke, I tried this :
var all_forms = document.querySelectorAll('[data-shortcode="contact_form"] > form');

all_forms.forEach((form) => {
form.addEventListener('submit', (e) => {
e.preventDefault();

// array that holds all the validation errors

var error_messages = [];

// find the subject field of the form

var subject = form.subject.value;

if (subject.length < 3) {
error_messages.push('Subject has to be more then 3 characters');
}

// show the error messages

// Select the user-feedback div
const userFeedbackDiv = form.querySelector('.user_feedback');

// Create a new div to hold the error messages
const errorDiv = document.createElement('div');
errorDiv.classList.add('error');

// Append the error div to the user-feedback div
userFeedbackDiv.appendChild(errorDiv);

// Select the error div
const errorDivElement = form.querySelector('.error');

// Clear any existing content in the error div
errorDivElement.innerHTML = '';

// Loop through the error messages array and create HTML elements for each message
error_messages.forEach(errorMessage => {
// Create a new <p> element
const errorParagraph = document.createElement('p');

// Set the text content of the <p> element to the current error message
errorParagraph.textContent = errorMessage;

// Append the <p> element to the error div
errorDivElement.appendChild(errorParagraph);
});
})
})
var all_forms = document.querySelectorAll('[data-shortcode="contact_form"] > form');

all_forms.forEach((form) => {
form.addEventListener('submit', (e) => {
e.preventDefault();

// array that holds all the validation errors

var error_messages = [];

// find the subject field of the form

var subject = form.subject.value;

if (subject.length < 3) {
error_messages.push('Subject has to be more then 3 characters');
}

// show the error messages

// Select the user-feedback div
const userFeedbackDiv = form.querySelector('.user_feedback');

// Create a new div to hold the error messages
const errorDiv = document.createElement('div');
errorDiv.classList.add('error');

// Append the error div to the user-feedback div
userFeedbackDiv.appendChild(errorDiv);

// Select the error div
const errorDivElement = form.querySelector('.error');

// Clear any existing content in the error div
errorDivElement.innerHTML = '';

// Loop through the error messages array and create HTML elements for each message
error_messages.forEach(errorMessage => {
// Create a new <p> element
const errorParagraph = document.createElement('p');

// Set the text content of the <p> element to the current error message
errorParagraph.textContent = errorMessage;

// Append the <p> element to the error div
errorDivElement.appendChild(errorParagraph);
});
})
})
` but then I see a message that the errorDiv is null So please some help ?
roelof
roelofOP15mo ago
js validation is working see this:
No description
roelof
roelofOP15mo ago
I only need your help on taking care that it is on the right form And how I can take care that the data is not send to the backend when the client validation fails
ἔρως
ἔρως15mo ago
sorry, super busy at work and house chores you should show everything in a list also, white on red is painful to read you should try the colors that bootstrap 5 or tailwind use you take care of the right form by using the form variable. you stop the form from submitting by using e.preventDefault(); there are 3 things you can do now: 1- submit to the same page 2- submit to ajax using an action 3- submit to an hardcoded url
roelof
roelofOP15mo ago
np this can wait I use the form variable already but then nothing works as I posted the latest code here you mean I have to choose how to submit from javascrript or do we talk about submitting to the backend I have tried number 3 and I think that is the easiest choice we can then reuse the code in the process directory
ἔρως
ἔρως15mo ago
yes yes it's the 2nd worst option, in my opinion
roelof
roelofOP15mo ago
oops I was hoping it was the best choice
ἔρως
ἔρως15mo ago
but all options have negative effects anyway there is no best
roelof
roelofOP15mo ago
this better with the colors :
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
a little
roelof
roelofOP15mo ago
but as I said first I hope to solve that the second form also shows the errors messages I cannot make that work 😢
ἔρως
ἔρως15mo ago
try the colors from that this is going to need a tiny rewrite
roelof
roelofOP15mo ago
oke I was already thinking of that
ἔρως
ἔρως15mo ago
it's not what you think
roelof
roelofOP15mo ago
I tried to solve it with the current code but could not make it work as I wanted
ἔρως
ἔρως15mo ago
your code has 1 issue: all the names are the same for all elements
roelof
roelofOP15mo ago
oke, so we have to do the same as we did with the id's ? so a change in the html of the form ?
ἔρως
ἔρως15mo ago
did you push the code?
roelof
roelofOP15mo ago
I thought i did but to be certain I push the code again
roelof
roelofOP15mo ago
I tried that already today and see then this message :
Uncaught TypeError: userFeedbackDiv is null
Uncaught TypeError: userFeedbackDiv is null
const userFeedbackDiv = form.querySelector('.user_feedback');
const userFeedbackDiv = form.querySelector('.user_feedback');
roelof
roelofOP15mo ago
can that be because the user-feedback is outside the form ?
ἔρως
ἔρως15mo ago
from inside the form, you're trying to get an element that's outside the form, right?
roelof
roelofOP15mo ago
yes and when I do this inside the code works vey fine and that costed me a whole day to find out 😢
ἔρως
ἔρως15mo ago
you spent 1 day to learn that you need to pay attention where things are in the dom you can't just chuck it to the f*ck it bucket and expect it to work everything has to be carefully laid out and you need to think about what you do
roelof
roelofOP15mo ago
Then we can think how we can make it work that if the client validaton fails there must be send no data to the server
ἔρως
ἔρως15mo ago
but that only comes from trying that should be what happens now
roelof
roelofOP15mo ago
yep, you learn the best by trying, failing and try again
ἔρως
ἔρως15mo ago
exactly
roelof
roelofOP15mo ago
but it makes it also very very frustatiing because I doubt then a lot about myself
ἔρως
ἔρως15mo ago
that's why you need to learn how to debug and you can only learn to debug when you learn to pay attention to your code
roelof
roelofOP15mo ago
I think I made the same "mistake"" then yesterday when you asked me about the form thing I still thinking how to solve it in js but you mean look at css
ἔρως
ἔρως15mo ago
that's because you're mixing stuff
roelof
roelofOP15mo ago
and getting stuck at a way I think this can be solved and forget that there are more ways to solve a problem
ἔρως
ἔρως15mo ago
most important you forget to stop and look at what you did and what you're trying to do and by not doing that, you implement the wrong "fix" - but actually add different bugs
roelof
roelofOP15mo ago
yep
ἔρως
ἔρως15mo ago
by the way, im not "shitting" on you or something or trying to bully you
roelof
roelofOP15mo ago
for the jsdoc stuff I have to dive into how that works Like I said i never worked with jsdoc or phpdoc I know
ἔρως
ἔρως15mo ago
im showing you what you did wrong, why it is wrong, how you can improve and then you can fix it without me you should, it will help you massively
roelof
roelofOP15mo ago
and I really appriciate the time and patience that you have for me
ἔρως
ἔρως15mo ago
if you type /**, vscode should type a bit for you you're welcome
roelof
roelofOP15mo ago
you are the first real person who really try to help me to get into wordpress development
ἔρως
ἔρως15mo ago
this is just general development, with some sprinkles of wordpress wordpress is just php, but with extra bloat and functions and i did learn some stuff too for example, i didn't knew you could register the styles and css, instead of enqueing
roelof
roelofOP15mo ago
That is developing. You never stop learning things like css changes a lot
ἔρως
ἔρως15mo ago
precisely
roelof
roelofOP15mo ago
and there will be new versions of js or php who does things different Or like wordpress the developers change the ide from pure php to react (gutenberg)
ἔρως
ἔρως15mo ago
i strongly dislike the guttenberg stuff, but metabox makes it easy to write blocks
roelof
roelofOP15mo ago
I make last month as a challenge my first block and I found it not very difficult But I think if you look at it, you stll find a lot of issues
ἔρως
ἔρως15mo ago
probably, but that's how everything is
roelof
roelofOP15mo ago
still googling what to put here :
/**
*
*/

var error_messages = [];
/**
*
*/

var error_messages = [];
just @type arrray<string> ?
ἔρως
ἔρως15mo ago
https://jsdoc.app/tags-type check the examples
roelof
roelofOP15mo ago
I did and I think it schould be
/**
* @type string[]
*/

var error_messages = [];
/**
* @type string[]
*/

var error_messages = [];
pff discord messed up my indentation 😦
ἔρως
ἔρως15mo ago
can you take a screenshot of the mouse hovering the variable name?
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
well, that's not proper jsdoc syntax im surprised it works
roelof
roelofOP15mo ago
😦
ἔρως
ἔρως15mo ago
/** @type {(string|Array.<string>)} */
var foo;
/** @type {(string|Array.<string>)} */
var foo;
that's the example
roelof
roelofOP15mo ago
oke changed it to
* @type {string[]}
* @type {string[]}
`
ἔρως
ἔρως15mo ago
that's still not proper syntax
roelof
roelofOP15mo ago
hmm
ἔρως
ἔρως15mo ago
look at the example
roelof
roelofOP15mo ago
I was looking at this ;
You can also indicate an array by appending [] to the type that is contained in the array. For example, the expression string[] indicates an array of strings.
You can also indicate an array by appending [] to the type that is contained in the array. For example, the expression string[] indicates an array of strings.
ἔρως
ἔρως15mo ago
look at what you have i know, but get into good habits
roelof
roelofOP15mo ago
yep, you example says I could be a string or a array of strings
ἔρως
ἔρως15mo ago
yup that's right
roelof
roelofOP15mo ago
in my code it can only the the array of strings
ἔρως
ἔρως15mo ago
yes, that's right too
roelof
roelofOP15mo ago
wait, I can also be empty
ἔρως
ἔρως15mo ago
an empty array is still an array it's an array of strings, with 0 or more strings
roelof
roelofOP15mo ago
and according to the text I can use string [] to indicate that it is a array of strings
ἔρως
ἔρως15mo ago
yes, you can other languages use it
roelof
roelofOP15mo ago
so sorry, I do not see what I do wrong
ἔρως
ἔρως15mo ago
well, the "proper" syntax is the horrid Array.<String> that's all it's not wrong what you've done
roelof
roelofOP15mo ago
oke, changed it and see no difference on the hover but to go back to my question
ἔρως
ἔρως15mo ago
you shouldn't you did nothing wrong, by the way
roelof
roelofOP15mo ago
what is then "better" then using a fixed url for the submitting to the server for the server validation when the client validation is a success oke, I got the feeling I did something wrong
ἔρως
ἔρως15mo ago
well, you didn't do anything wrong
roelof
roelofOP15mo ago
I was hoping we could re-use my process/index.php file
ἔρως
ἔρως15mo ago
well, i wouldn't keep that name
roelof
roelofOP15mo ago
that can easily be changed but you said of the 3 that was the 2th second worst solution I was also looking at that ajax thing but im totally do not have knowlegde or xp with it
ἔρως
ἔρως15mo ago
all solutions have downsides
roelof
roelofOP15mo ago
as always 🙂
ἔρως
ἔρως15mo ago
these are the solutions: 1- submit to the same page 2- submit to ajax using an action 3- submit to an hardcoded url there's no "best" solution there's the worst one, which is #2
roelof
roelofOP15mo ago
and with 1 , you do everything in 1 file and I like so seperate things so still Im thinking of 3
ἔρως
ἔρως15mo ago
sadly, it's the best option for you, that is i would implement 1 and 3, but, it's for a challenge, so, 3
roelof
roelofOP15mo ago
with 1 you make more functions in the mycustomForm.php file ? or in the form.php file ?
ἔρως
ἔρως15mo ago
or in a functions.php file and you use the same function to submit
roelof
roelofOP15mo ago
oke, but for the challenge that would not work bcause I cannot send the functions.php with the submission so yes, 3 is then the best option
ἔρως
ἔρως15mo ago
it actually works, because when you submit to yourself, you can detect it
roelof
roelofOP15mo ago
??
roelof
roelofOP15mo ago
I do not like that part
ἔρως
ἔρως15mo ago
calm down
roelof
roelofOP15mo ago
because I cannot return more then 1 validation error
ἔρως
ἔρως15mo ago
breathe lets implement the javascript to submit first
roelof
roelofOP15mo ago
oke, I only tell what I see what im thinking what is wrong with my current soluton ?? I thought we did with the addeventlisteners
ἔρως
ἔρως15mo ago
no, that's to do client-side validation
roelof
roelofOP15mo ago
oke, I wait and see right now , I have no idea what your way of working is
ἔρως
ἔρως15mo ago
i just go with whatever there is, because i can use anything
roelof
roelofOP15mo ago
oke, but you were talking about js to submit first
ἔρως
ἔρως15mo ago
yes
roelof
roelofOP15mo ago
and I rather use plain js becuase im most familiar with
ἔρως
ἔρως15mo ago
and for that, you can use either xhr or fetch i recommend fetch
roelof
roelofOP15mo ago
yes. I know js fetch with a lot of .then 🙂
ἔρως
ἔρως15mo ago
you can make it a lot easier
roelof
roelofOP15mo ago
but with fetch we need a url and we cannot know on which url the site is where he plugin is used yep, you can use await 🙂
ἔρως
ἔρως15mo ago
this is where we can be sneaky you can set the form action to the url you want to submit the form to
roelof
roelofOP15mo ago
yes I have seen that
ἔρως
ἔρως15mo ago
you can also get the plugin url with wordpress
roelof
roelofOP15mo ago
in my old code I use process/index.php as action yes, with plugin_dir
ἔρως
ἔρως15mo ago
now, we can do the same yup or something like that
roelof
roelofOP15mo ago
oke still no idea how the action and js fetch work together so im waiting
ἔρως
ἔρως15mo ago
basically, you use fetch to send the post request to the url in the form action
roelof
roelofOP15mo ago
sorry, sounds still abracadabra to me
ἔρως
ἔρως15mo ago
if you read it slowly, you will see that i gave you the answer
roelof
roelofOP15mo ago
if you do not mind, I will sleep about it
ἔρως
ἔρως15mo ago
i dont mind it at all
roelof
roelofOP15mo ago
Right now I still not see it at all
ἔρως
ἔρως15mo ago
it's fine sleep on it it's a shitty pillow, but it's fine
roelof
roelofOP15mo ago
I know but my brain in not working properly because im more tired then yesterday
ἔρως
ἔρως15mo ago
i understood, it's fine just rest
roelof
roelofOP15mo ago
and even with google. I do not see how fetch and the action can work together
ἔρως
ἔρως15mo ago
tomorrow, you will see
roelof
roelofOP15mo ago
I hope. Most of the examples I find is using admin.php to handle this
ἔρως
ἔρως15mo ago
i know, but that isn't a good option
roelof
roelofOP15mo ago
GN and I see you tomorrow afternoon or tomorrow evening I think I use the morning to sleep a lot
ἔρως
ἔρως15mo ago
goodnight, and rest well
roelof
roelofOP15mo ago
you too
ἔρως
ἔρως15mo ago
thank you
roelof
roelofOP15mo ago
YW or do you mean something like this :

if (error_message.length === 0) {
fetch(' ')
}

if (error_message.length === 0) {
fetch(' ')
}
and that in the js file ?
ἔρως
ἔρως15mo ago
something close to that, yes
roelof
roelofOP15mo ago
pfff, again almost
ἔρως
ἔρως15mo ago
yes invert the logic and your code will be more readable also, since you will never have a negative length, you just need to check if it is falsy or truthy
roelof
roelofOP15mo ago
oke So I can do :
if (error_messages.length) {
do something
} else {
fetch ...
}
if (error_messages.length) {
do something
} else {
fetch ...
}
ἔρως
ἔρως15mo ago
no, you need to check the length if the length is truthy, just return
roelof
roelofOP15mo ago
changed it now better ?
ἔρως
ἔρως15mo ago
if you show the messages and return, you dont need the else
roelof
roelofOP15mo ago
oke So like this :

if (error_messages.length) {
//show error messages
return
}

fetch ....

if (error_messages.length) {
//show error messages
return
}

fetch ....
and I think you want a post fetch ?
ἔρως
ἔρως15mo ago
yup you should actually have the code to show the errors in a function
roelof
roelofOP15mo ago
oke, that schould not be a big problem and it looks I need some sort of Formdata so we can use it in the fetch and figure out the url of the fetch I seem to remember you do not liked my old formdata function
function submit_contact_form()
{
var fd = new FormData();
fd.append('subject',$(".subject").val());
fd.append('email',$(".email).val());
fd.append('message',$(".message").val());


}
function submit_contact_form()
{
var fd = new FormData();
fd.append('subject',$(".subject").val());
fd.append('email',$(".email).val());
fd.append('message',$(".message").val());


}
I do not like it because it is jquery
ἔρως
ἔρως15mo ago
you can create a form data by passing the form into the constructor
roelof
roelofOP15mo ago
constructor ?? We do not have one at the moment as far as I know I think you are going faster then I can
ἔρως
ἔρως15mo ago
try checking the mdn page for the formdata object you will understand what i meam mwan mean
roelof
roelofOP15mo ago
yep As I read it fast , It the same as I did in my function
var fs = new Formdata()
var fs = new Formdata()
ἔρως
ἔρως15mo ago
yes, but you can pass a form to it
roelof
roelofOP15mo ago
yep, I saw it on another mdn page but there they used a class and our form does not have a class or id or can I just do
var formdata = new FormData(form)
var formdata = new FormData(form)
because we know that form contains one of the forms on the page
ἔρως
ἔρως15mo ago
all it needs is a form with inputs
roelof
roelofOP15mo ago
???
ἔρως
ἔρως15mo ago
the formdata only needs a form
roelof
roelofOP15mo ago
I thougth the form variable holds only the form
ἔρως
ἔρως15mo ago
yes, it does it shoukd should and the formdata is what you send over fetch
roelof
roelofOP15mo ago
oke so I have now this :
if (error_messages.length) {
showErrorMessages(error_messages, form);
return
}

var formdata = new FormData(form)

var data = await fetch('' , {
method: 'POST',
body: formdata
})

var respone = await data.json();
if (error_messages.length) {
showErrorMessages(error_messages, form);
return
}

var formdata = new FormData(form)

var data = await fetch('' , {
method: 'POST',
body: formdata
})

var respone = await data.json();
I did not fill in the url because you did not like the ./response/index.php url
ἔρως
ἔρως15mo ago
you cant await: the function needs to be async
roelof
roelofOP15mo ago
yes. I saw the errror message VS code makes now the whole loop async but I think that is not good
ἔρως
ἔρως15mo ago
send here the code
roelof
roelofOP15mo ago
code is pushed to the repo
ἔρως
ἔρως15mo ago
i cant see it now, only in about 1-2 hours
roelof
roelofOP15mo ago
o wierd
ἔρως
ἔρως15mo ago
im on my phone and working
roelof
roelofOP15mo ago
roelof
roelofOP15mo ago
We can wait till your home and have eating
ἔρως
ἔρως15mo ago
im always at home
roelof
roelofOP15mo ago
oke, me too except on Mondag an Friday Morning
roelof
roelofOP15mo ago
or I can do it like this which I think it is a cleaner solution
roelof
roelofOP15mo ago
Which one do you like the most ?
ἔρως
ἔρως15mo ago
async function send_to_backend(data) {
var data = await fetch('' , {
method: 'POST',
body: formdata
})
var respone = await data.json();
return response;
}
async function send_to_backend(data) {
var data = await fetch('' , {
method: 'POST',
body: formdata
})
var respone = await data.json();
return response;
}
this won't work
var formdata = new FormData(form)

var data = await fetch('' , {
method: 'POST',
body: formdata
})

var respone = await data.json();
var formdata = new FormData(form)

var data = await fetch('' , {
method: 'POST',
body: formdata
})

var respone = await data.json();
^ this will probably work
roelof
roelofOP15mo ago
oke, so we should go for the second approach then I also hope we will agree on the url
ἔρως
ἔρως15mo ago
the url is kinda up to you but in this case, it just needs to be the form action
roelof
roelofOP15mo ago
oke, and the form action is now still #
ἔρως
ἔρως15mo ago
yes, but you can change it
roelof
roelofOP15mo ago
maybe make a file called backend-validation.php ?
ἔρως
ἔρως15mo ago
it's not validation you'll be submitting
roelof
roelofOP15mo ago
oke, submit-backend.php ?
ἔρως
ἔρως15mo ago
i wouldn't include "backend" on any name of anything
roelof
roelofOP15mo ago
hmm, then at the moment i out of ideas for a better name but if I enter it on the action is my fetch then working ?
ἔρως
ἔρως15mo ago
not yet there's something you can improve
roelof
roelofOP15mo ago
pfff and again no idea what
ἔρως
ἔρως15mo ago
you're creating a form data - which has all the values from all inputs but you're also accessing all inputs manually
roelof
roelofOP15mo ago
I wonder more and more if this is something for me Maybe I should stick with just frontend wordpress things with gutenberg
ἔρως
ἔρως15mo ago
so, why don't you use the formdata and get the values from there? instead of javascript and php, you will do javascript and php?
roelof
roelofOP15mo ago
no php, just some json work on theme.json and just click and add the things I need For my first template I do not need any php or javascript but how do you mean use the formdata from there In the backend validation I will using that data
ἔρως
ἔρως15mo ago
dude, forget that the backend exists, for now just front-end instead of you manually getting the values for all the inputs, for the validation, you can use the formdata
roelof
roelofOP15mo ago
oke, I could but then I think I have to rewrite the js
ἔρως
ἔρως15mo ago
barely
roelof
roelofOP15mo ago
because right now the formdata is used when all the validation is a success
ἔρως
ἔρως15mo ago
yes, but you can move it to the top
roelof
roelofOP15mo ago
if (error_messages.length) {
showErrorMessages(error_messages, form);
return
}

var formdata = new FormData(form)

var backend_response = send_to_backend(formdata);
if (error_messages.length) {
showErrorMessages(error_messages, form);
return
}

var formdata = new FormData(form)

var backend_response = send_to_backend(formdata);
oke I think it can be then I have to rewrite this part to :
var subject = form.subject.value;
var email = form.email.value;
var message = form.message.value;
var subject = form.subject.value;
var email = form.email.value;
var message = form.message.value;
ἔρως
ἔρως15mo ago
yup, you just need to grab the values from the formdata
roelof
roelofOP15mo ago
I think I need to see how the formdata looks like
ἔρως
ἔρως15mo ago
you should read the mdn page about it
roelof
roelofOP15mo ago
oke When I use this , it seems to work:
var subject = formdata.get("subject");
var email = formdata.get("email");
var message = formdata.get("message");
var subject = formdata.get("subject");
var email = formdata.get("email");
var message = formdata.get("message");
` I see the validatiion errors again
ἔρως
ἔρως15mo ago
it should nice!
roelof
roelofOP15mo ago
tomorrow work on the server validation ? Or am I going to fast?
ἔρως
ἔρως15mo ago
tomorrow, it is server side validation
roelof
roelofOP15mo ago
Oké could i name it submit.php
ἔρως
ἔρως15mo ago
that's a much better name
roelof
roelofOP15mo ago
oke, Them I will make a file called submit.php in that fle sanitaze the formData and make a check I have to see how I then make a error-message when the validation fails I can use WPError and return that mabe GN
ἔρως
ἔρως15mo ago
you must use a wp_error object, and then you simply send it in json otherwise, send a success message goodnight
roelof
roelofOP15mo ago
I think I can send a success message when the mail is send . not earlier
ἔρως
ἔρως15mo ago
you either send an error before sending the email an error after sending the email and it fails or a success after the email sends
roelof
roelofOP15mo ago
Can I do this :
var data = await fetch( "<?php echo plugin_dir_url( __FILE__ ); ?>/submit.php", {
var data = await fetch( "<?php echo plugin_dir_url( __FILE__ ); ?>/submit.php", {
ἔρως
ἔρως15mo ago
no, you cant thats why you use the form action
roelof
roelofOP15mo ago
oke, and how do I do that ? the form action now looks like this :
<form method="post" action="<?php echo plugin_dir_url( __FILE__ ); ?>/submit.php">
<form method="post" action="<?php echo plugin_dir_url( __FILE__ ); ?>/submit.php">
` Did some google this better :
var data = await fetch( form.action, {
var data = await fetch( form.action, {
ἔρως
ἔρως15mo ago
that is much better almost: the first argument is the path to the file inside the plugin directory by using __FILE__, you are referring to the template file
roelof
roelofOP15mo ago
oke, so it should be __dir__
ἔρως
ἔρως15mo ago
nope
roelof
roelofOP15mo ago
if that is good , then I have to find out in which format the submit function gets the data
ἔρως
ἔρως15mo ago
^ should be that it gets as an x-url-encoded string
roelof
roelofOP15mo ago
yes, it is in the root of the plugin directory
ἔρως
ἔρως15mo ago
then you need to put that
roelof
roelofOP15mo ago
sorry, but this looks good to me :
<form method="post" action="http://myblog.test/wp-content/plugins/mycustomForm//submit.php">

<form method="post" action="http://myblog.test/wp-content/plugins/mycustomForm//submit.php">

ἔρως
ἔρως15mo ago
you need to remove the slash
roelof
roelofOP15mo ago
oke done
ἔρως
ἔρως15mo ago
that should be it
roelof
roelofOP15mo ago
oke, then now find out how that x-url encoded string looks like
ἔρως
ἔρως15mo ago
that is what the fetch sends
roelof
roelofOP15mo ago
so I know how I can make this part works :
$name = sanitize_text_field($_POST['name']);
$name = sanitize_text_field($_POST['name']);
in submit.php
ἔρως
ἔρως15mo ago
yes but you need to load the wordpress environment
roelof
roelofOP15mo ago
yep, like this :
require_once($path."wp-load.php");
require_once($path."wp-load.php");
ἔρως
ἔρως15mo ago
yup, depending on where $path is
roelof
roelofOP15mo ago
path was on the tutorial this:
$path = preg_replace('/wp-content.*$/','',__DIR__);
$path = preg_replace('/wp-content.*$/','',__DIR__);
ἔρως
ἔρως15mo ago
that should work
roelof
roelofOP15mo ago
oke, then now google how I can get the contents out of that x-url encoded string I have no idea because that has no variabe that holds things
ἔρως
ἔρως15mo ago
just ignore it for now try to submit
roelof
roelofOP15mo ago
??? I thought I did with this code :
async function send_to_backend(data, form) {
var data = await fetch( form.action, {
method: 'POST',
body: data
})
var response = await data.json();
}
async function send_to_backend(data, form) {
var data = await fetch( form.action, {
method: 'POST',
body: data
})
var response = await data.json();
}
ἔρως
ἔρως15mo ago
does it work?
roelof
roelofOP15mo ago
nope, something is broke the form is all messed up and now its looking well and submitting is not working
Uncaught (in promise) SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
Uncaught (in promise) SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
not a very clear message 😦 or could it be that submit.php does not send anything back
ἔρως
ἔρως15mo ago
then it works it means that whatever is being sent isnt json
roelof
roelofOP15mo ago
yep, it is this line
var response = await data.json();
var response = await data.json();
it is expecting json back from the submit.php file but so far am I not in the code when I delete that line. I see no error messags but also nothing seems to be happening and a status of 200 in the network tab so the submittng seems to work
roelof
roelofOP15mo ago
and I see this
No description
roelof
roelofOP15mo ago
which also looks well to me
ἔρως
ἔρως15mo ago
what did it send?
roelof
roelofOP15mo ago
??? it send the data that I put into the form in text/html format if I may believe the headers
ἔρως
ἔρως15mo ago
dont believe the headers
roelof
roelofOP15mo ago
oke
ἔρως
ἔρως15mo ago
what's returned from the server?
roelof
roelofOP15mo ago
so it I understand things right I can use things like
Formdata.subject
Formdata.subject
sorry, I now have the guess the answer No idea where you are heading to sorry
ἔρως
ἔρως15mo ago
im just heading for you to check that the server isnt returning an error
roelof
roelofOP15mo ago
it does not like I said earlier the server gives back a 200 oke respons
ἔρως
ἔρως15mo ago
and what else?
roelof
roelofOP15mo ago
sorry, still no idea what you try to ask and what schould be the headers
ἔρως
ἔρως15mo ago
not the headers the content the headers can say it is a png or even that it has no content
roelof
roelofOP15mo ago
oke but what that have to do with reading the values of the form in submit.php I try to solve that "problem"
ἔρως
ἔρως15mo ago
you already did that once you send the formdata, its done the server does it all automatically
roelof
roelofOP15mo ago
oke,
ἔρως
ἔρως15mo ago
you just have to use the $_POST variable thats it
roelof
roelofOP15mo ago
Still confused how then to make this work :
$name = sanitize_text_field($_POST['name']);
$name = sanitize_text_field($_POST['name']);
`
ἔρως
ἔρως15mo ago
thats it also, trim the result but, you should only do it when presenting the text if you're not presenting, just stop dont use that function
roelof
roelofOP15mo ago
oke de requirements said that I have to use that function I have to sanitaze anything Then I can do :
$subject - trim($_POST['subject']) ;
$subject - trim($_POST['subject']) ;
and how did I do so far :
<?php

$path = preg_replace('/wp-content.*$/','',__DIR__);
require_once($path."wp-load.php");

$subject = trim($_POST['subject']) ;
$email = trim($_POST['email']);
$message = trim($_POST['message']);

$errors = new WP_Error();

if (strlen($subject) < 2 ) {
$errors->add('Error' , "subject has to be more then 2 characters");
}

if (!is_email($emai)) {
$errors->add("Error" , "Please provide a valid email");
}

if (strlen($message) < 2 ) {
$errors->add('Error' , "message has to be more then 2 characters");
}
<?php

$path = preg_replace('/wp-content.*$/','',__DIR__);
require_once($path."wp-load.php");

$subject = trim($_POST['subject']) ;
$email = trim($_POST['email']);
$message = trim($_POST['message']);

$errors = new WP_Error();

if (strlen($subject) < 2 ) {
$errors->add('Error' , "subject has to be more then 2 characters");
}

if (!is_email($emai)) {
$errors->add("Error" , "Please provide a valid email");
}

if (strlen($message) < 2 ) {
$errors->add('Error' , "message has to be more then 2 characters");
}
if it's allright, I have to figure out how to convert this to json and send it back to the fetch and figure out how to handle the json from there Because in the end I can be two things A Error or a Success time for a break
ἔρως
ἔρως15mo ago
you can send data with wp_send_json
roelof
roelofOP15mo ago
found that out Code looks now like this :
<?php

$path = preg_replace('/wp-content.*$/','',__DIR__);
require_once($path."wp-load.php");

$subject = trim($_POST['subject']) ;
$email = trim($_POST['email']);
$message = trim($_POST['message']);

$errors = new WP_Error();

if (strlen($subject) < 2 ) {
$errors->add('Error' , "subject has to be more then 2 characters");
}

if (!is_email($emai)) {
$errors->add("Error" , "Please provide a valid email");
}

if (strlen($message) < 2 ) {
$errors->add('Error' , "message has to be more then 2 characters");
}

if ($errors->has_errors()) {
return wp_send_json_error( $errors );
}
<?php

$path = preg_replace('/wp-content.*$/','',__DIR__);
require_once($path."wp-load.php");

$subject = trim($_POST['subject']) ;
$email = trim($_POST['email']);
$message = trim($_POST['message']);

$errors = new WP_Error();

if (strlen($subject) < 2 ) {
$errors->add('Error' , "subject has to be more then 2 characters");
}

if (!is_email($emai)) {
$errors->add("Error" , "Please provide a valid email");
}

if (strlen($message) < 2 ) {
$errors->add('Error' , "message has to be more then 2 characters");
}

if ($errors->has_errors()) {
return wp_send_json_error( $errors );
}
` now think how to handle things in the fetch part
ἔρως
ἔρως15mo ago
check the data being sent from the server then adapt it to fit the function that shows all the errors
roelof
roelofOP15mo ago
oke but as I said the response can be a Error json or if the mail is send a Success I mean I have to figure out which 2 it is The errrors schould be in red and suceess in green oops, I forget to check the nonce back to google to see how I can check that one
ἔρως
ἔρως15mo ago
wp_check_nonce or something like that first, make sure you understand how an error message looks like then worry about the success message then worry about implementing in javascripf
roelof
roelofOP15mo ago
that is what I try to do First think hpw to display the response here
async function send_to_backend(data, form) {
var data = await fetch( form.action, {
method: 'POST',
body: data
})
var response = await data.json();
}
async function send_to_backend(data, form) {
var data = await fetch( form.action, {
method: 'POST',
body: data
})
var response = await data.json();
}
or here :
var backend_response = send_to_backend(formdata, form);
var backend_response = send_to_backend(formdata, form);
\
roelof
roelofOP15mo ago
chips. still something is wrong here :
No description
roelof
roelofOP15mo ago
code :
<?php

$path = preg_replace('/wp-content.*$/','',__DIR__);
require_once($path."wp-load.php");

$subject = trim($_POST['subject']) ;
$email = trim($_POST['email']);
$message = trim($_POST['message']);

$errors = new WP_Error();

if (strlen($subject) < 2 ) {
$errors->add('Error' , "subject has to be more then 2 characters");
}

if (!is_email($emai)) {
$errors->add("Error" , "Please provide a valid email");
}

if (strlen($message) < 2 ) {
$errors->add('Error' , "message has to be more then 2 characters");
}

if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'submit_contact_form' ) )
{
$errors->add('Error' , 'Form is messed up');

}

if ($errors->has_errors()) {
return wp_send_json_error( $errors );
}
<?php

$path = preg_replace('/wp-content.*$/','',__DIR__);
require_once($path."wp-load.php");

$subject = trim($_POST['subject']) ;
$email = trim($_POST['email']);
$message = trim($_POST['message']);

$errors = new WP_Error();

if (strlen($subject) < 2 ) {
$errors->add('Error' , "subject has to be more then 2 characters");
}

if (!is_email($emai)) {
$errors->add("Error" , "Please provide a valid email");
}

if (strlen($message) < 2 ) {
$errors->add('Error' , "message has to be more then 2 characters");
}

if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'submit_contact_form' ) )
{
$errors->add('Error' , 'Form is messed up');

}

if ($errors->has_errors()) {
return wp_send_json_error( $errors );
}
and there has to be a error because I deleted the nonce in the form and again no clue where things go wrong and the error message does also not give any clue any clues ?? hmm It seems that the nonce check is not working no idea why not There is at the moment no nonce avaible
ἔρως
ἔρως15mo ago
have you checked what the server is returning?
roelof
roelofOP15mo ago
as I looked at the response tab it seems to return the whole page
roelof
roelofOP15mo ago
No description
roelof
roelofOP15mo ago
makes me more and more confused I have said it has to return the errors object
ἔρως
ἔρως15mo ago
whats the action in the form? show it in the element inspector
roelof
roelofOP15mo ago
I also did this :

print_r($POST);

if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], 'submit_contact_form')) {
$errors->add('Error', 'Form is messed up');
}

print_r($errors);

print_r($POST);

if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], 'submit_contact_form')) {
$errors->add('Error', 'Form is messed up');
}

print_r($errors);
` but do not see any output
ἔρως
ἔρως15mo ago
did you checked the action?
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
its correct there can you add a console.log before the fetch? and show the form action there?
roelof
roelofOP15mo ago
he. when I click on the url I see this :
No description
ἔρως
ἔρως15mo ago
thats later
roelof
roelofOP15mo ago
Then I see this :
No description
roelof
roelofOP15mo ago
so looks good to me
ἔρως
ἔρως15mo ago
is the website in http or https?
roelof
roelofOP15mo ago
I think http
ἔρως
ἔρως15mo ago
then can you check?
roelof
roelofOP15mo ago
if I look at this page it is http
roelof
roelofOP15mo ago
Laragon - portable, isolated, fast & powerful universal development environment for PHP, Node.js, Python, Java, Go, Ruby.
Pretty URLs
One of useful features of Laragon is Auto Virutal Hosts Just put your project (ex. mysite) in the Document Root (C:\laragon\www) and click “Reload“ button, you’ll have this pretty url:http://mysite.te
ἔρως
ἔρως15mo ago
if you click to edit the website url, is it http or https?
roelof
roelofOP15mo ago
http if I change it to https I get a message that the site is not safe
ἔρως
ἔρως15mo ago
thats fine
roelof
roelofOP15mo ago
🙂 and somehow it looks like this line is not good
$subject = trim($_POST['subject']);
$subject = trim($_POST['subject']);
ἔρως
ἔρως15mo ago
thats because it will throw an error if it doesnt exist you need to protect your code the same way you did with the $atts array you can even apply the trim to all values in the post variable
roelof
roelofOP15mo ago
oke, so use esc_attr ?
ἔρως
ἔρως15mo ago
no thats sanitization
roelof
roelofOP15mo ago
or do you mean this :
$atts = array_replace(["class" => "", "subject" => "", "email" => "", "message" => ""], $args['atts']);
$atts = array_replace(["class" => "", "subject" => "", "email" => "", "message" => ""], $args['atts']);
but why do I have to protect it. I check the values after it and when it's not valid , I make error messages sorry, I totally do not understand what this has to do with that the json returned is not in a good format
ἔρως
ἔρως15mo ago
well, you have to assume that $_POST is empty and that means that it can have anything you have to make sure that it has the keys that you need
roelof
roelofOP15mo ago
oke, so just as thet say never trust a input from a user
ἔρως
ἔρως15mo ago
yes also, garbage in garbage out that's why you don't sanitize just cut it from the root
roelof
roelofOP15mo ago
oke, so i have do again the array_replace thing ?
ἔρως
ἔρως15mo ago
if it is invalid, it's garbage yup
roelof
roelofOP15mo ago
hmm, then I have to think about the last argument
ἔρως
ἔρως15mo ago
what last argument? there's 2, and they are both arrays
roelof
roelofOP15mo ago
or can I do something like this :

$atts = array_replace(["subject" => "", "email" => "", "message" => ""], $_POST);

$atts = array_replace(["subject" => "", "email" => "", "message" => ""], $_POST);

?
ἔρως
ἔρως15mo ago
yup
roelof
roelofOP15mo ago
oke So I can do :
$atts = array_replace(["subject" => "", "email" => "", "message" => ""], $_POST);

$subject = trim($atts['subject']);
$email = trim($atts['email']);
$message = trim($atts['message']);
$atts = array_replace(["subject" => "", "email" => "", "message" => ""], $_POST);

$subject = trim($atts['subject']);
$email = trim($atts['email']);
$message = trim($atts['message']);
ἔρως
ἔρως15mo ago
well, atts is a terrible name for this situation, that is
roelof
roelofOP15mo ago
oke
ἔρως
ἔρως15mo ago
also, why are you repeating the code?
roelof
roelofOP15mo ago
I want to make it work first Do I repeat things ? so the last part can be deleted ?
ἔρως
ἔρως15mo ago
$subject = trim($atts['subject']);
$email = trim($atts['email']);
$message = trim($atts['message']);
$subject = trim($atts['subject']);
$email = trim($atts['email']);
$message = trim($atts['message']);
^ repetition
roelof
roelofOP15mo ago
because wanted the values into variables so I can use that in the checks
ἔρως
ἔρως15mo ago
you already have the values in variables
roelof
roelofOP15mo ago
oke , so I can delete it
ἔρως
ἔρως15mo ago
the variables are, currently, $atts and $atts and $atts
roelof
roelofOP15mo ago
oops. that is not good
ἔρως
ἔρως15mo ago
yes, you're repeating
roelof
roelofOP15mo ago
as far as I know I can do this :
[$subject, $email, $message] = array_replace(["subject" => "", "email" => "", "message" => ""], $_POST);
[$subject, $email, $message] = array_replace(["subject" => "", "email" => "", "message" => ""], $_POST);
ἔρως
ἔρως15mo ago
that's not valid php
roelof
roelofOP15mo ago
hmm, do we not miss the nonce thing
ἔρως
ἔρως15mo ago
yes
roelof
roelofOP15mo ago
I think I call it a day This makes me very frustating and very stress full And that is something I do not need at the moment
ἔρως
ἔρως15mo ago
i know what you mean, just take a break and relax
roelof
roelofOP15mo ago
I will and will think if this is what I really wanted You are a good teacher but all the problems makes me crazy I think this challenge is more then I can chew
ἔρως
ἔρως15mo ago
you lack practice and experience that's all you've improved on your google skills
roelof
roelofOP15mo ago
oke, but I still do not see why the array destructing is not good
ἔρως
ἔρως15mo ago
but with experience, you will be able to do this basic stuff by yourself
roelof
roelofOP15mo ago
Looks good to me when I google
ἔρως
ἔρως15mo ago
because it doesn't exist in php (at least not 7.4 and older) there's a proposal for the syntax, but don't think they implemented everything in it also, why are you separating all the array keys into variables? that's something i see lots of people do, more often than not for no reason at all
roelof
roelofOP15mo ago
as I said earlier. then I can check the variables if theyt have a good length or if they are a valid email
ἔρως
ἔρως15mo ago
okay, but lets imagine that the array destruction assignment works
roelof
roelofOP15mo ago
like I do here :
if (strlen($subject) < 2) {
$errors->add('Error', "subject has to be more then 2 characters");
}
if (strlen($subject) < 2) {
$errors->add('Error', "subject has to be more then 2 characters");
}
ἔρως
ἔρως15mo ago
what do you expect to happen if there's 5 keys? or 2 keys? or 50 keys?
roelof
roelofOP15mo ago
Then I have a very big problem
ἔρως
ἔρως15mo ago
why $subject instead of $array['subject']? why duplicate values all over the place?
roelof
roelofOP15mo ago
wtith 5 I can make it work the same way but with 50 it will be a very very big line
ἔρως
ἔρως15mo ago
you have to remember that you have absolutely no control at all what-so-ever in what you receive
roelof
roelofOP15mo ago
oke, so lets name it $array instead of $atts ? I do not, even if the form has fixed fields ?
ἔρως
ἔρως15mo ago
i would call it data or post or request_data or post_data or something more explicit anyone can send anything to your script, including nothing at all
roelof
roelofOP15mo ago
pfff, I see more an more that on waht he calls junior challenge has a lot of things I would not think of
ἔρως
ἔρως15mo ago
this is an intermediate challenge, in my opinion
roelof
roelofOP15mo ago
but im not a junior yet and no idea if I ever will be a junior
ἔρως
ἔρως15mo ago
well, that's up to you do you want to finish or give up?
roelof
roelofOP15mo ago
for the intermediate, He makes it that the fields has multiple unknows fields and store it on a custom object
ἔρως
ἔρως15mo ago
that's a tiny bit more advanced
roelof
roelofOP15mo ago
oke so for $subject I can now do $data['subject'] for me a challenge I do not want to try first learn to make a custom object with fixed fields before I m going to try on dynamic fields maybe in a few months
ἔρως
ἔρως15mo ago
maybe, but you need practice
roelof
roelofOP15mo ago
a lot of practice I have now this :
<?php

$path = preg_replace('/wp-content.*$/', '', __DIR__);
require_once($path . "wp-load.php");

$data = array_replace(["subject" => "", "email" => "", "message" => ""], $_POST);

$errors = new WP_Error();

if (strlen($data['subject']) < 2) {
$errors->add('Error', "subject has to be more then 2 characters");
}

if (!is_email($data['emai'])) {
$errors->add("Error", "Please provide a valid email");
}

if (strlen($data['message']) < 2) {
$errors->add('Error', "message has to be more then 2 characters");
}

if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], 'submit_contact_form')) {
$errors->add('Error', 'Form is messed up');
}



if ($errors->has_errors()) {
return wp_send_json_error($errors);
}
<?php

$path = preg_replace('/wp-content.*$/', '', __DIR__);
require_once($path . "wp-load.php");

$data = array_replace(["subject" => "", "email" => "", "message" => ""], $_POST);

$errors = new WP_Error();

if (strlen($data['subject']) < 2) {
$errors->add('Error', "subject has to be more then 2 characters");
}

if (!is_email($data['emai'])) {
$errors->add("Error", "Please provide a valid email");
}

if (strlen($data['message']) < 2) {
$errors->add('Error', "message has to be more then 2 characters");
}

if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], 'submit_contact_form')) {
$errors->add('Error', 'Form is messed up');
}



if ($errors->has_errors()) {
return wp_send_json_error($errors);
}
` I need to add that nonce part somewhere in the array_replace and figure out the right way to check it but for now I will take a break tiill tomorrow afternoon if I have then energy
ἔρως
ἔρως15mo ago
strlen($data['subject']) <-- this has to be with the mb_strlen function strlen basically counts bytes if someone sends a poop emoji, it's 1 character, but strlen counts as 4
roelof
roelofOP15mo ago
changed on both places
ἔρως
ἔρως15mo ago
$data['emai'] <-- watch out for misspells install the cspell plugin to avoid those https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.code-spell-checker im not telling you this to speak bad of your english
roelof
roelofOP15mo ago
Can I do this :
`
$data = array_replace(["subject" => "", "email" => "", "message" => "", nonce=""], $_POST);
`
$data = array_replace(["subject" => "", "email" => "", "message" => "", nonce=""], $_POST);
and then change the check to this :
if (!isset($data['nonce') || !wp_verify_nonce($data['nonce', 'submit_contact_form')) {
if (!isset($data['nonce') || !wp_verify_nonce($data['nonce', 'submit_contact_form')) {
ἔρως
ἔρως15mo ago
im telling you this because you will have bugs that take hours to debug, just to see that there's something wrong i would verify the nonce anyways, even if it is empty to avoid timming attacks
roelof
roelofOP15mo ago
??? And how do I do that
ἔρως
ἔρως15mo ago
wp_verify_nonce($data['nonce'], 'submit_contact_form') <-- just this
roelof
roelofOP15mo ago
as I see it I verify it in the if then ?
ἔρως
ἔρως15mo ago
yes and in fact, you should verify it at the top wait, no verify at the bottom then it is possible to do a timming attack on the nonce
roelof
roelofOP15mo ago
should I not be !wp-verify ,,,
ἔρως
ἔρως15mo ago
yes, it should
roelof
roelofOP15mo ago
because I want to place a error when it's failing
ἔρως
ἔρως15mo ago
just verify it at the bottom
roelof
roelofOP15mo ago
oke, then we have this part
if ($errors->has_errors()) {
return wp_send_json_error($errors);
}
if ($errors->has_errors()) {
return wp_send_json_error($errors);
}
is this good ?
ἔρως
ἔρως15mo ago
you don't need the return, but yes
roelof
roelofOP15mo ago
I wanted to check if the $errors has a value but it is a WP-error obejct
ἔρως
ἔρως15mo ago
but, make ALL your errors translatable this is perfect
roelof
roelofOP15mo ago
this looks better :
http://myblog.test/wp-content/plugins/mycustomForm/submit.php mycustomForm.js:37:13
Promise { <state>: "fulfilled", <value>: undefined }

<state>: "fulfilled"

<value>: undefined

<prototype>: Promise.prototype { … }
http://myblog.test/wp-content/plugins/mycustomForm/submit.php mycustomForm.js:37:13
Promise { <state>: "fulfilled", <value>: undefined }

<state>: "fulfilled"

<value>: undefined

<prototype>: Promise.prototype { … }
ἔρως
ἔρως15mo ago
nice, it works!
roelof
roelofOP15mo ago
yep, no error messages again now I can look how response looks like 🙂
ἔρως
ἔρως15mo ago
probably responsive enough
roelof
roelofOP15mo ago
damned
ἔρως
ἔρως15mo ago
but, you didn't finish the backend stuff
roelof
roelofOP15mo ago
the old error back
Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
`
ἔρως
ἔρως15mo ago
that's what i told you
roelof
roelofOP15mo ago
oke, I was expecting a message that the nonce is not good because the nonce is not existing if I see that I was thinking about the part of sending the email
ἔρως
ἔρως15mo ago
what did you get?
roelof
roelofOP15mo ago
that stupid annoying error that the json is not good
ἔρως
ἔρως15mo ago
what did the server sent?
roelof
roelofOP15mo ago
this :
roelof
roelofOP15mo ago
No description
roelof
roelofOP15mo ago
a response object
ἔρως
ἔρως15mo ago
that's not what you get from the server that's what you get from fetch
roelof
roelofOP15mo ago
oke
ἔρως
ἔρως15mo ago
what's in the network tab?
roelof
roelofOP15mo ago
this
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
what's in the 3rd tab?
roelof
roelofOP15mo ago
this:
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
and the 4th tab?
roelof
roelofOP15mo ago
so in English : no payload for this request
ἔρως
ἔρως15mo ago
that's weird, but okay
roelof
roelofOP15mo ago
the form :
No description
roelof
roelofOP15mo ago
also very very wierd
ἔρως
ἔρως15mo ago
so, there's something wrong with the server
roelof
roelofOP15mo ago
oke, big problem I use laragon and that one makes the server and so on
ἔρως
ἔρως15mo ago
if you access the url directly, what do you get?
roelof
roelofOP15mo ago
what is the url directly ?
ἔρως
ἔρως15mo ago
this
No description
ἔρως
ἔρως15mo ago
go to that url
roelof
roelofOP15mo ago
then I see this :
No description
roelof
roelofOP15mo ago
so it looking everything is then wrong 😦 this looks better
roelof
roelofOP15mo ago
No description
roelof
roelofOP15mo ago
but when I try it on the form
I see the same wierd outcomes
ἔρως
ἔρως15mo ago
🤔 did you try making sure it is a post request?
roelof
roelofOP15mo ago
yes
async function send_to_backend(data, form) {
var data = await fetch( form.action, {
method: 'POST',
body: data
})
var response = await data;
console.log(response);
}
async function send_to_backend(data, form) {
var data = await fetch( form.action, {
method: 'POST',
body: data
})
var response = await data;
console.log(response);
}
ἔρως
ἔρως15mo ago
console.log() the form.action
roelof
roelofOP15mo ago

<form method="post" action="<?php echo plugin_dir_url( __DIR__ ); ?>submit.php">

<form method="post" action="<?php echo plugin_dir_url( __DIR__ ); ?>submit.php">
ἔρως
ἔρως15mo ago
that's not how you use that function https://developer.wordpress.org/reference/functions/plugin_dir_url/ read the documentation on it
roelof
roelofOP15mo ago
form action is :
http://myblog.test/wp-content/plugins/mycustomForm/submit.php
http://myblog.test/wp-content/plugins/mycustomForm/submit.php
ἔρως
ἔρως15mo ago
you will see the error right away just read it
roelof
roelofOP15mo ago
I did and I think it schould be :
<form method="post" action="<?php echo plugin_dir_url( __DIR__ ) . "submit.php"; ?>>
<form method="post" action="<?php echo plugin_dir_url( __DIR__ ) . "submit.php"; ?>>
ἔρως
ἔρως15mo ago
what's the name of the first argument?
roelof
roelofOP15mo ago
the file name
ἔρως
ἔρως15mo ago
and what are you passing?
roelof
roelofOP15mo ago
A DIR
ἔρως
ἔρως15mo ago
yup, which is "wrong" it "works", but isn't what you want
roelof
roelofOP15mo ago
oke When I do this :
<form method="post" action="<?php echo plugin_dir_url( 'submit.php' ) ?>">;
<form method="post" action="<?php echo plugin_dir_url( 'submit.php' ) ?>">;
I see on the 3th tab my data and on the 4th tab nothing
ἔρως
ἔρως15mo ago
you have an error here: the extra ; show me
roelof
roelofOP15mo ago
3th tab
roelof
roelofOP15mo ago
No description
roelof
roelofOP15mo ago
4th tab
roelof
roelofOP15mo ago
No description
roelof
roelofOP15mo ago
not good the submit is gone from the action part so back to the manual
ἔρως
ἔρως15mo ago
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
^ add this https://stackoverflow.com/questions/35325370/how-do-i-post-a-x-www-form-urlencoded-request-using-fetch <-- took it from there i hate the fetch weird thingy, always gives some sort of issue
roelof
roelofOP15mo ago
I call it a day I have this as action
<form method="post" action = "<?php echo plugin_dir_url( 'submit.php') ?>">
<form method="post" action = "<?php echo plugin_dir_url( 'submit.php') ?>">
and it gives me on the 3th tab
roelof
roelofOP15mo ago
No description
roelof
roelofOP15mo ago
and this one the 4 th tab
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
that is bad data im surprised that php didnt return an error 400
roelof
roelofOP15mo ago
im still thinking that the action is wrong but I have no clue what it schould be at the moment
ἔρως
ἔρως15mo ago
no, it shouldn't be
roelof
roelofOP15mo ago
so this is good :
<form method="post" action = "<?php echo plugin_dir_url( 'submit.php') ?>">
<form method="post" action = "<?php echo plugin_dir_url( 'submit.php') ?>">
when I look in my html the submit is totally missing from the action and I saw this example
echo plugin_dir_url( __DIR__ ).'images/placeholder.png';
echo plugin_dir_url( __DIR__ ).'images/placeholder.png';
but then I get a 404 wierd and stupid php server
ἔρως
ἔρως15mo ago
no, it's you who's being a dummy but that's due to lack of practice
roelof
roelofOP15mo ago
thanks for the compliment 😛
ἔρως
ἔρως15mo ago
<form method="post" action = "<?php echo plugin_dir_url( 'submit.php') ?>"> <-- stop putting whitespaces all over the attributes <form method="post" action="<?php echo plugin_dir_url( 'submit.php') ?>"> <-- this stop putting __dir__ inside the function echo plugin_dir_url( 'images/placeholder.png' ); <-- this
roelof
roelofOP15mo ago
that is a example of the page you linked too
ἔρως
ἔρως15mo ago
you have to read the documentation and pay attention, specially to the examples wait, sorry you're right, im wrong i taught you wrong
roelof
roelofOP15mo ago
oke
roelof
roelofOP15mo ago
your action gives me back this :
No description
ἔρως
ἔρως15mo ago
you had it right before that's still wrong data
roelof
roelofOP15mo ago
yep but what is now the right one ?
ἔρως
ἔρως15mo ago
show me the generated html just the form html
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
yeah, the action is wrong probably because of what i told you <form method="post" action="<?php echo plugin_dir_url( __FILE__ ), 'submit.php' ?>"> but, better yet
roelof
roelofOP15mo ago
and whenI do that
ἔρως
ἔρως15mo ago
<form method="post" action="<?php echo esc_attr(plugin_dir_url( __FILE__ ) . 'submit.php') ?>"> ^ use that
roelof
roelofOP15mo ago
then I get a 404
ἔρως
ἔρως15mo ago
show the html
roelof
roelofOP15mo ago
and when I change it do __DIR__ I see the form back as answerr
ἔρως
ἔρως15mo ago
alright, show me the html
roelof
roelofOP15mo ago
oke
roelof
roelofOP15mo ago
No description
roelof
roelofOP15mo ago
it now adding a templates part which is not good
ἔρως
ἔρως15mo ago
that's the wrong url
roelof
roelofOP15mo ago
<form method="post" action="<?php echo plugin_dir_url(__FILE__) . 'submit.php' ?>">
<form method="post" action="<?php echo plugin_dir_url(__FILE__) . 'submit.php' ?>">
roelof
roelofOP15mo ago
and when I change to __DIR__
I got this as url
No description
ἔρως
ἔρως15mo ago
then use whatever produces this
roelof
roelofOP15mo ago
but then the 4th tab is showing garbish very very wierd and it makes totally no sense to me but as I said I take a very very big break
ἔρως
ἔρως15mo ago
it's showing garbage because of the fetch did you push?
roelof
roelofOP15mo ago
nope, I can push to the repo
ἔρως
ἔρως15mo ago
push it
roelof
roelofOP15mo ago
did it
ἔρως
ἔρως15mo ago
async function send_to_backend(data, form) {
console.log(form.action);
var data = await fetch( form.action, {
method: 'POST',
body: data,
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
})
var response = await data;
console.log(response);
}
async function send_to_backend(data, form) {
console.log(form.action);
var data = await fetch( form.action, {
method: 'POST',
body: data,
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
})
var response = await data;
console.log(response);
}
the problem is here
async function send_to_backend(data, url) {
var data = await fetch( url, {
method: 'POST',
body: data
})
var response = await data;
console.log(response);
}
async function send_to_backend(data, url) {
var data = await fetch( url, {
method: 'POST',
body: data
})
var response = await data;
console.log(response);
}
try this
roelof
roelofOP15mo ago
sorry a 404
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
that's because the url is not a string you're passing a form element
roelof
roelofOP15mo ago
oke, so I have to hardcode the url ? because in myCustomForm.js the url is not known
ἔρως
ἔρως15mo ago
no, you just pass the form.action as the 2nd argument
roelof
roelofOP15mo ago
yes, that was the trick I now see the error mesage I expect
roelof
roelofOP15mo ago
No description
roelof
roelofOP15mo ago
finally tomomrow on my list Make the email Try to send it because right now I know that everything is oke
ἔρως
ἔρως15mo ago
well, making the email is stupid easy
roelof
roelofOP15mo ago
I know I think I can borrow the code from the tutorial
/* write email information for sending to admin */
$subject = 'Idea Pro Contact Form Submitted';
$message = '';

$message .= 'Name: '.$name.' <br />';
$message .= 'Email: '.$email.'<br />';
$message .= 'Phone: '.$phone.'<br />';

$comments = wpautop($comments);
$comments = str_replace("<p>","",$comments);
$comments = str_replace("</p>","<br /><br />",$comments);

$message .= 'Comments:<br />'.$comments.'<br /><br />';

$message .= 'Thank you.';

wp_mail($to,$subject,$message);
/* write email information for sending to admin */
$subject = 'Idea Pro Contact Form Submitted';
$message = '';

$message .= 'Name: '.$name.' <br />';
$message .= 'Email: '.$email.'<br />';
$message .= 'Phone: '.$phone.'<br />';

$comments = wpautop($comments);
$comments = str_replace("<p>","",$comments);
$comments = str_replace("</p>","<br /><br />",$comments);

$message .= 'Comments:<br />'.$comments.'<br /><br />';

$message .= 'Thank you.';

wp_mail($to,$subject,$message);
ἔρως
ἔρως15mo ago
NO BAD BAD BAD
roelof
roelofOP15mo ago
then check if the mail is really send and send that back to the client oke, then I have to find a better tutorial or explanation to make the email
ἔρως
ἔρως15mo ago
you do the same thing you did to render the form but you use the output for the email
roelof
roelofOP15mo ago
oke, so make a html template for the email Also that I have to find out how to do that
ἔρως
ἔρως15mo ago
the exact same thing you did for the form but you use it for the email wp_mail($to,$subject,$message); <-- then use this to send the email
roelof
roelofOP15mo ago
I hit the shower and goto bed Otherwise Im not awake when I have to goto work tomorrow morning to work on learning to make block themes
ἔρως
ἔρως15mo ago
dude, you should hit the shower and go to bed but you aint learning any "block themes" tomorrow
roelof
roelofOP15mo ago
im not ? How do you know what I do at "work"
roelof
roelofOP15mo ago
IM work on converting a html template to a block template
ἔρως
ἔρως15mo ago
at work? yeah, you do you but im talking about this thingy you're doing
roelof
roelofOP15mo ago
at home I work on this annoying problem
ἔρως
ἔρως15mo ago
🤣 "annoying"
roelof
roelofOP15mo ago
that will be tomorrow afternoon, evening or in the weekend I have to see how many energy I have at any moment GN
ἔρως
ἔρως15mo ago
you're almost done don't give up on the finishing line
roelof
roelofOP15mo ago
yep, annoying too much things that eats up at lot of time I won't but as I said I have to see how my energy is at any time to see what I do at a moment
ἔρως
ἔρως15mo ago
it does, but that's lack of practice and experience
roelof
roelofOP15mo ago
and a good mentor who teaches me things step by step pty you do not like react otherwise we could look at my first challenge where I had to make a custom componet which displays a testimonal Card IM sure it cannot work with multiple Cards and I know it will not work when placing in a group GN
ἔρως
ἔρως15mo ago
it's not that i don't like react, it's just that there's better things for me to learn, like svelte and im always so tired after work goodnight
roelof
roelofOP15mo ago
Svelte I tried also , just liek Vue and React Svelte has the most readable code , Never tried the new version where the introduce runes it this good
wp_mail(get_option( 'admin_email' ),$email, load_template(__DIR__ . '/templates/email.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode' => $shortcode_tag,
'prefix' => $shortcode_tag . $counter,
]));
wp_mail(get_option( 'admin_email' ),$email, load_template(__DIR__ . '/templates/email.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode' => $shortcode_tag,
'prefix' => $shortcode_tag . $counter,
]));
ἔρως
ἔρως15mo ago
almost the variables will be different you need to pass it the post data
roelof
roelofOP15mo ago
So change $atts to data and do we really need the content part ?
ἔρως
ἔρως15mo ago
no, it will be empty you wont need the prefix either
roelof
roelofOP15mo ago
oke
wp_mail(get_option( 'admin_email' ),$email, load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data,
'shortcode' => $shortcode_tag,
]));
wp_mail(get_option( 'admin_email' ),$email, load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data,
'shortcode' => $shortcode_tag,
]));
?
ἔρως
ἔρως15mo ago
looks good to me but load_template doesnt return anything you have to capture the output oh, and YOU MUST WRITE THE EMAIL IN HTML4 WITH LITTLE/NO CSS assuming you care about compatibility with garbage outlook
roelof
roelofOP15mo ago
I know I have to capture the output Can I do :
$email_send = wp-mail ....
$email_send = wp-mail ....
then for the email I then have to find a html4 template THe form uses a lot of css
ἔρως
ἔρως15mo ago
yes, you can do thay that you should look for a good email template
roelof
roelofOP15mo ago
What do you think of this one http://leemunroe.github.io/responsive-html-email-template/email.html we can use it I think without the button and some other text
ἔρως
ἔρως15mo ago
so much css try to send that html in an email to an outlook account
roelof
roelofOP15mo ago
oke, then I have to look further Take a break and could not find a better template email templates are a hell because every email reader has other things that they can or cannot do
ἔρως
ἔρως15mo ago
yup and in the end, you have to implement with tables because of outlook
roelof
roelofOP15mo ago
Chips i forget hoe to do that with tables. I like flex more So i really hope we Will find something I connotatie find anyrhing
ἔρως
ἔρως15mo ago
its stupid hard to make emails so, just do a single-page website, basically
roelof
roelofOP15mo ago
oke, but the challenge said we have to send the data by email Could we not make a plain text one as the code from the tutorial does so stuck again and that with the flnisch in sight
ἔρως
ἔρως15mo ago
you can have a plain text email, but that is ugly af you could also make something super basic in pure html too
roelof
roelofOP15mo ago
I know I have to think how I can make that super basic one
ἔρως
ἔρως15mo ago
a table thats it
roelof
roelofOP15mo ago
I found a lot of nice ones but thet all use inlne css toa make it look better oke so a table where at once site the name of the property is and on the other side the data entered I think without some css it is still loking ugly
ἔρως
ἔρως15mo ago
that is fine but test it yourself many look like absolute 🍑 on outlook
roelof
roelofOP15mo ago
oke
roelof
roelofOP15mo ago
what do you think of this one then : https://codepen.io/timtoews/pen/bGGZjdL
Tim Töws
CodePen
Simple Mail Template
Create a tiny simple mail template. ...
ἔρως
ἔρως15mo ago
it's nothing spectacularly gorgeous or anything, but, it seems alright lots of inline css, but, it's fine
roelof
roelofOP15mo ago
oke, if you find here a better one we use that : https://codepen.io/tag/email-template?cursor=ZD0xJm89MCZwPTM=
ἔρως
ἔρως15mo ago
https://jsfiddle.net/yfx7r9jp/1/ <-- if you need inspiration on how to layout with tables and styling with font
Kevin Powell's challenge #1 - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
ἔρως
ἔρως15mo ago
over 700 lines for not a whole lot :/ it has a whole css reset in it it looks good, but writting in that will be absolutely miserable
roelof
roelofOP15mo ago
We can use a sort of card
ἔρως
ἔρως15mo ago
how about this one?
roelof
roelofOP15mo ago
I have seen that one too Could be also fun
ἔρως
ἔρως15mo ago
i can see this being a lot easier than all others
roelof
roelofOP15mo ago
have to think how to make the text Maybe something of this : as header ;

You send the following message to me :

{{ message }


You send the following message to me :

{{ message }

the buitton we do not need because I do not want any action of the user
ἔρως
ἔρως15mo ago
🤔 you can just use variables
roelof
roelofOP15mo ago
Yes i could instead the name use the email variabele and add the subject somehow. Have to think hoe because my english is not so good
ἔρως
ἔρως15mo ago
well, you have the data sent from the form
roelof
roelofOP15mo ago
Yep i know Like i said earlier how to use these in a good english sentence(s)
ἔρως
ἔρως15mo ago
just write lorem ipsum
roelof
roelofOP15mo ago
Lol
ἔρως
ἔρως15mo ago
hey, you gotta start somewhere
roelof
roelofOP15mo ago
That is right
ἔρως
ἔρως15mo ago
so, just go for it
roelof
roelofOP15mo ago
Oké boss
ἔρως
ἔρως15mo ago
🫡
roelof
roelofOP15mo ago
And still think i m a 7 student?
ἔρως
ἔρως15mo ago
i do, yes you search stuff, accept advice, improve, implement, get frustrated and rest, but don't give up
roelof
roelofOP15mo ago
Happy to hear Like i said earlier myself a lot Not much self cofidience
ἔρως
ἔρως15mo ago
well, you won't be confident until you do it more often
roelof
roelofOP15mo ago
oke
ἔρως
ἔρως15mo ago
just keep practicing
roelof
roelofOP15mo ago
oke
roelof
roelofOP15mo ago
I wrote the firs attempt to make the email pretty. I hope I did a good job
roelof
roelofOP15mo ago
For me time to sleep
ἔρως
ἔρως15mo ago
@import url(https://fonts.googleapis.com/css?family=Rubik:300,400,500,700|Open+Sans:300,400,600,700); <-- do not use this of all the bad ideas, that one is one of the worst it isn't your fault, but it's horrible
roelof
roelofOP15mo ago
oke, I just copied it from the example but it is deleted
ἔρως
ἔρως15mo ago
i know i didn't notice it
roelof
roelofOP15mo ago
np
ἔρως
ἔρως15mo ago
fonts like that should never be part of emails also, google fonts violates gdpr google uses the ips and sells that data to advertisers and others
roelof
roelofOP15mo ago
oke, never learned that
ἔρως
ἔρως15mo ago
a court in germany rulled against google on that
roelof
roelofOP15mo ago
when I solve some Front End Mentors challenge , it is told to include google fonts in the css or in the head
ἔρως
ἔρως15mo ago
you can leave the font-family as it is that's for convenience
roelof
roelofOP15mo ago
oke so next would be to set up a mail server and try to send a email tomorrow ?
ἔρως
ἔρως15mo ago
this is why google now offers an option to download the fonts 🤣 😂 are you insane??? 😂 just use fakemail
roelof
roelofOP15mo ago
no im pleasantly disturbed. is that a plugin ?
ἔρως
ἔρως15mo ago
no, it isnt it's an exe that fakes being an email server
roelof
roelofOP15mo ago
oke, just another round to google
ἔρως
ἔρως15mo ago
xampp already comes with it maybe you can find it online and set it up
roelof
roelofOP15mo ago
I do not use xammp but laragon
ἔρως
ἔρως15mo ago
i know
roelof
roelofOP15mo ago
hmm there is something wrong with the nonce thing

<?php wp_nonce_field'submit_contact_form', 'contact_form_nonce'); ?>(

<?php wp_nonce_field'submit_contact_form', 'contact_form_nonce'); ?>(
ἔρως
ἔρως15mo ago
yes, it's missing a (
roelof
roelofOP15mo ago
oke this was the test:
if (!wp_verify_nonce($data['nonce'], 'submit_contact_form')) {
$errors->add('Error', 'Form is messed up');
}
if (!wp_verify_nonce($data['nonce'], 'submit_contact_form')) {
$errors->add('Error', 'Form is messed up');
}
ἔρως
ἔρως15mo ago
🤣
roelof
roelofOP15mo ago
time to sleep I still do not see it
<?php wp_nonce_field('submit_contact_form', 'contact_form_nonce'); ?>
<?php wp_nonce_field('submit_contact_form', 'contact_form_nonce'); ?>
roelof
roelofOP15mo ago
I will do it tomorrow maybe I see it ., Right now I totally do not see what I did wrong
ἔρως
ἔρως15mo ago
the name of the field is different
roelof
roelofOP15mo ago
really time to sleep
<?php wp_nonce_field(); ?>
<?php wp_nonce_field(); ?>
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], $_POST);
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], $_POST);
if (!wp_verify_nonce($data['_wpnonce'], 'submit_contact_form')) {
$errors->add('Error', 'Form is messed up');
}
if (!wp_verify_nonce($data['_wpnonce'], 'submit_contact_form')) {
$errors->add('Error', 'Form is messed up');
}
ἔρως
ἔρως15mo ago
that should be it
roelof
roelofOP15mo ago
still the same stupid error
ἔρως
ἔρως15mo ago
but you're forgetting the first argument of wp_nonce_field
roelof
roelofOP15mo ago
???
ἔρως
ἔρως15mo ago
the action
roelof
roelofOP15mo ago
oke I think I got that now and now a lot of other errors to solve tomorrow
warning: Undefined variable $email in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php on line 30

Warning: Undefined variable $atts in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php on line 31

Warning: Undefined variable $content in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php on line 32

Warning: Undefined variable $shortcode_tag in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php on line 33

Warning: Undefined variable $shortcode_tag in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php on line 34

Warning: Undefined variable $counter in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php on line 34

Fatal error: Uncaught TypeError: array_replace(): Argument #2 must be of type array, null given in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php:3 Stack trace: #0 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php(3): array_replace(Array, NULL) #1 C:\laragon\www\myBlog\wp-includes\template.php(792): require('C:\\laragon\\www\\...') #2 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php(34): load_template('C:\\laragon\\www\\...', false, Array) #3 {main} thrown in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php on line 3
warning: Undefined variable $email in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php on line 30

Warning: Undefined variable $atts in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php on line 31

Warning: Undefined variable $content in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php on line 32

Warning: Undefined variable $shortcode_tag in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php on line 33

Warning: Undefined variable $shortcode_tag in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php on line 34

Warning: Undefined variable $counter in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php on line 34

Fatal error: Uncaught TypeError: array_replace(): Argument #2 must be of type array, null given in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php:3 Stack trace: #0 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php(3): array_replace(Array, NULL) #1 C:\laragon\www\myBlog\wp-includes\template.php(792): require('C:\\laragon\\www\\...') #2 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php(34): load_template('C:\\laragon\\www\\...', false, Array) #3 {main} thrown in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php on line 3
ἔρως
ἔρως15mo ago
yes, for tomorrow its easier than you think
roelof
roelofOP15mo ago
yes I made a error here :
<?php

$data = array_replace(["class" => "", "subject" => "", "email" => "", "message" => ""], $args['data']);
$prefix = esc_attr($args['prefix']);
$shortcode = esc_attr($args['shortcode'])
?>
<?php

$data = array_replace(["class" => "", "subject" => "", "email" => "", "message" => ""], $args['data']);
$prefix = esc_attr($args['prefix']);
$shortcode = esc_attr($args['shortcode'])
?>
ἔρως
ἔρως15mo ago
yup that's one
roelof
roelofOP15mo ago
the args['data'] is wrong have to think tomorrow well what it schould be
ἔρως
ἔρως15mo ago
it's what you pass to the template function
roelof
roelofOP15mo ago
that would be a data variable if I did this well
<?php

$data = array_replace(["class" => "", "subject" => "", "email" => "", "message" => ""], $args['data']);
$prefix = esc_attr($args['prefix']);
$shortcode = esc_attr($args['shortcode'])
?>
<?php

$data = array_replace(["class" => "", "subject" => "", "email" => "", "message" => ""], $args['data']);
$prefix = esc_attr($args['prefix']);
$shortcode = esc_attr($args['shortcode'])
?>
ἔρως
ἔρως15mo ago
that's out of context, but looks like it
roelof
roelofOP15mo ago
I mean to paste this :


wp_mail(get_option( 'admin_email' ), $email, load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data]
));


wp_mail(get_option( 'admin_email' ), $email, load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data]
));
ἔρως
ἔρως15mo ago
yes, you did it right $prefix = esc_attr($args['prefix']); $shortcode = esc_attr($args['shortcode']) <-- except this
roelof
roelofOP15mo ago
??? it fails on the array_replace on the second argument is null
ἔρως
ἔρως15mo ago
that should be right
roelof
roelofOP15mo ago
so it looks to me that $args['data'] is wrong somehow
ἔρως
ἔρως15mo ago
$data has to be an array
roelof
roelofOP15mo ago
: Argument #2 must be of type array, null given in
: Argument #2 must be of type array, null given in
ἔρως
ἔρως15mo ago
that's what i said here
roelof
roelofOP15mo ago
sorry , it late the error is talking about this :
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], $_POST);
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], $_POST);
Fatal error: Uncaught TypeError: array_replace(): Argument #2 must be of type array, null given in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php:3 Stack trace: #0 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php(3): array_replace(Array, NULL) #1 C:\laragon\www\myBlog\wp-includes\template.php(792): require('C:\\laragon\\www\\...') #2 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php(34): load_template('C:\\laragon\\www\\...', false, Array) #3 {main} thrown in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php on line 3
Fatal error: Uncaught TypeError: array_replace(): Argument #2 must be of type array, null given in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php:3 Stack trace: #0 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php(3): array_replace(Array, NULL) #1 C:\laragon\www\myBlog\wp-includes\template.php(792): require('C:\\laragon\\www\\...') #2 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php(34): load_template('C:\\laragon\\www\\...', false, Array) #3 {main} thrown in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php on line 3
ἔρως
ἔρως15mo ago
that's kinda right
roelof
roelofOP15mo ago
im totally confused it is for certain this one :
$data = array_replace(["class" => "", "subject" => "", "email" => "", "message" => ""], $data['data']);
$data = array_replace(["class" => "", "subject" => "", "email" => "", "message" => ""], $data['data']);
ἔρως
ἔρως15mo ago
$data doesn't exist on line 3 of email.php
roelof
roelofOP15mo ago
really time to sleep oke but $args also not
ἔρως
ἔρως15mo ago
$args has to exist $args['data'] may not
roelof
roelofOP15mo ago
oke tomrrow I will look what it should be
ἔρως
ἔρως15mo ago
just do a var_dump of everything
roelof
roelofOP15mo ago
so
var_dump($args)
var_dump($args)
??? I will do tomorrow I almost fall asleep behind the computer
ἔρως
ἔρως15mo ago
just go to sleep you won't do anything today i say to do not give up, but im not telling you to torture yourself
roelof
roelofOP15mo ago
I know
ἔρως
ἔρως15mo ago
then go to sleep
roelof
roelofOP15mo ago
It in my character to want to solve it now
ἔρως
ἔρως15mo ago
rest well, and we do it tomorrow you won't solve it now
roelof
roelofOP15mo ago
I cannot stand that its not working
ἔρως
ἔρως15mo ago
you will waffle about like an uncooked chunk of batter YOU HAVE TO it's important to learn to stop and this is your cue to stop stopping, resting and solving that's how it works
roelof
roelofOP15mo ago
awake after a bad night because of the proffesional help that I got yesterday But this looks good to me :
array(1) { ["data"]=> array(5) { ["subject"]=> string(5) "test " ["email"]=> string(19) "[email protected]" ["message"]=> string(5) "test " ["_wpnonce"]=> string(10) "06fc2ee77c" ["_wp_http_referer"]=> string(1) "/" } }
array(1) { ["data"]=> array(5) { ["subject"]=> string(5) "test " ["email"]=> string(19) "[email protected]" ["message"]=> string(5) "test " ["_wpnonce"]=> string(10) "06fc2ee77c" ["_wp_http_referer"]=> string(1) "/" } }
this is the args variable oke, data looks also well to me
array(6) { ["class"]=> string(0) "" ["subject"]=> string(5) "test " ["email"]=> string(19) "[email protected]" ["message"]=> string(5) "test " ["_wpnonce"]=> string(10) "06fc2ee77c" ["_wp_http_referer"]=> string(1) "/" }
array(6) { ["class"]=> string(0) "" ["subject"]=> string(5) "test " ["email"]=> string(19) "[email protected]" ["message"]=> string(5) "test " ["_wpnonce"]=> string(10) "06fc2ee77c" ["_wp_http_referer"]=> string(1) "/" }
so the problem is this line :

subject : <php echo $data['subject'] ?>
email: <php echo $data['email'] ?>
message: <php echo $data['message'] ?>

subject : <php echo $data['subject'] ?>
email: <php echo $data['email'] ?>
message: <php echo $data['message'] ?>
` it does not know email and alsp subject but it seems that it displays some empty string for it hmm, everything seems to work except I see this:
Warning: Undefined variable $email in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php on line 30
Warning: Undefined variable $email in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php on line 30
but there is no $email on that line there is no$email at all in the submit.php and wp_mail gives me :
*/bool(false)
*/bool(false)
but it is with a email catcher solved
roelof
roelofOP15mo ago
and I like it :
No description
roelof
roelofOP15mo ago
now find out why wp-mail fails and how I can make a fail or success json file and then find out how I can know if I need to display a fail or success message I think the best way would be the iterate the array or json and if that works clean up de css and make the toaster look nice and then finally ship it
ἔρως
ἔρως15mo ago
wp-mail probably fails because you dont have anything to send emails
roelof
roelofOP15mo ago
oke then I have to think how we return back a success or a fail/error message or we can :
json-decode(["Error", "Mail cannot be send"]) ;
json-decode(["Error", "Mail cannot be send"]) ;
ἔρως
ἔρως15mo ago
wp_send_json you have to shape the success message to look like the error message, but indicate a success
roelof
roelofOP15mo ago
oke, so like this :
if (mail-send) {
wp-send_json(["Success", "Mail has been send"]);
} else {
wp=send_json("Error, "Mail cannot be send"]);
}
if (mail-send) {
wp-send_json(["Success", "Mail has been send"]);
} else {
wp=send_json("Error, "Mail cannot be send"]);
}
and after that we can do :
wp-redirect([$data["refer"]);
wp-redirect([$data["refer"]);
and submit.php is also ready ?
ἔρως
ἔρως15mo ago
no, just send a wp_error when it fails and no, no redirect
roelof
roelofOP15mo ago
no redirect ?? he
roelof
roelofOP15mo ago
there is something wrong with when its not a success

if (!$mail_send) {
wp_send_json(["Error", "Mail cannot be send"]);
}

if (!$mail_send) {
wp_send_json(["Error", "Mail cannot be send"]);
}
No description
ἔρως
ἔρως15mo ago
that's the output of the email
roelof
roelofOP15mo ago
yes., I expect only the error message back
ἔρως
ἔρως15mo ago
can you show me the code?
roelof
roelofOP15mo ago
the whole file ? of course
<?php

$path = preg_replace('/wp-content.*$/', '', __DIR__);
require_once($path . "wp-load.php");

$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], $_POST);

$errors = new WP_Error();

if (mb_strlen($data['subject']) < 2) {
$errors->add('Error', "subject has to be more then 2 characters");
}

if (!is_email($data['email'])) {
$errors->add("Error", "Please provide a valid email");
}

if (mb_strlen($data['message']) < 2) {
$errors->add('Error', "message has to be more then 2 characters");
}

if (!wp_verify_nonce($data['_wpnonce'], 'submit_contact_form')) {
$errors->add('Error', 'Form is messed up');
}

if ($errors->has_errors()) {
wp_send_json_error($errors);
}

$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'], load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data
]
));

if (!$mail_send) {
wp_send_json(["Error", "Mail cannot be send"]);
}
<?php

$path = preg_replace('/wp-content.*$/', '', __DIR__);
require_once($path . "wp-load.php");

$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], $_POST);

$errors = new WP_Error();

if (mb_strlen($data['subject']) < 2) {
$errors->add('Error', "subject has to be more then 2 characters");
}

if (!is_email($data['email'])) {
$errors->add("Error", "Please provide a valid email");
}

if (mb_strlen($data['message']) < 2) {
$errors->add('Error', "message has to be more then 2 characters");
}

if (!wp_verify_nonce($data['_wpnonce'], 'submit_contact_form')) {
$errors->add('Error', 'Form is messed up');
}

if ($errors->has_errors()) {
wp_send_json_error($errors);
}

$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'], load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data
]
));

if (!$mail_send) {
wp_send_json(["Error", "Mail cannot be send"]);
}
ἔρως
ἔρως15mo ago
$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'], load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data
]
));
$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'], load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data
]
));
you have to capture the output and pass it to the email function
roelof
roelofOP15mo ago
oke, I thought I did it here
ἔρως
ἔρως15mo ago
nope, you forgot
roelof
roelofOP15mo ago
oke, then back to the manual to see how I can capture it
ἔρως
ἔρως15mo ago
we've did that before
roelof
roelofOP15mo ago
??? Did we As far as I know this is the first time we using wp_mail to send a mail time for dinner and mu brain is not doing what I hope. It's very foggy
ἔρως
ἔρως15mo ago
take a break, you moved forward on your own and that is really good yes, we did
roelof
roelofOP15mo ago
??? not with wp_mail as far as I know
ἔρως
ἔρως15mo ago
no, but we did before for something else
roelof
roelofOP15mo ago
got the feeling that you try to find me this :
async function send_to_backend(data, url) {
var data = await fetch( url, {
method: 'POST',
body: data
})
var response = await data;
console.log(response);
}
async function send_to_backend(data, url) {
var data = await fetch( url, {
method: 'POST',
body: data
})
var response = await data;
console.log(response);
}
because there we send something to the server
ἔρως
ἔρως15mo ago
nope where did you used the template function before?
roelof
roelofOP15mo ago
on 2 places if I have it right one is here in the head file :
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode' => $shortcode_tag,
'prefix' => $shortcode_tag . $counter,
]);
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode' => $shortcode_tag,
'prefix' => $shortcode_tag . $counter,
]);
and here :
$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'], load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data
]
));
$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'], load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data
]
));
but I think that is not what you asked me because we do not fetch the data or outcome so im still in the dark
ἔρως
ἔρως15mo ago
in this one, what's around the function call?
roelof
roelofOP15mo ago
a, ob_start and a ob_flush ? and that one is not on the email template thing
ἔρως
ἔρως15mo ago
yes yup, exactly but you have to use it, because the template function doesn't do it for you
roelof
roelofOP15mo ago
oke can I use it around the wp-mail ? Otherwise I have to think well how to change the wp-mail call to include that
ἔρως
ἔρως15mo ago
no, you cant you have to get the output before you call the mail function
roelof
roelofOP15mo ago
oke, more thinking to do
ἔρως
ἔρως15mo ago
it's not much thinking you just move it outside the mail function
roelof
roelofOP15mo ago
oke, but what schould I then place instead of it wp_mail needs the data to send
ἔρως
ἔρως15mo ago
a variable with the output
roelof
roelofOP15mo ago
oke, I will think and sleep about it Right now I have this :
ob_start();
load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data
]
ob_flush();


$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'],
));
ob_start();
load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data
]
ob_flush();


$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'],
));
but still fail totally to see how I can a variable here
ἔρως
ἔρως15mo ago
what's happening on this one?
roelof
roelofOP15mo ago
it loads and execute the template
ἔρως
ἔρως15mo ago
and what's around it?
roelof
roelofOP15mo ago
a buffer
ἔρως
ἔρως15mo ago
paste the code here
roelof
roelofOP15mo ago
which code ?
ἔρως
ἔρως15mo ago
the code around what's displaying the form
roelof
roelofOP15mo ago
I did that already
ἔρως
ἔρως15mo ago
i'm talking about the code around this
roelof
roelofOP15mo ago
yes Im too
ἔρως
ἔρως15mo ago
then paste it here
roelof
roelofOP15mo ago
that is this code :
ob_start();
load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data
]);
ob_flush();
ob_start();
load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data
]);
ob_flush();
ἔρως
ἔρως15mo ago
that's the email, not the form
roelof
roelofOP15mo ago
oke, sorry
ἔρως
ἔρως15mo ago
but i was going to ask for that code anyways
roelof
roelofOP15mo ago
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode' => $shortcode_tag,
'prefix' => $shortcode_tag . $counter,
]);
return ob_end_flush();
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode' => $shortcode_tag,
'prefix' => $shortcode_tag . $counter,
]);
return ob_end_flush();
ἔρως
ἔρως15mo ago
now, compare both what's different there?
roelof
roelofOP15mo ago
the "word" return is missing on the first one
ἔρως
ἔρως15mo ago
how about this: make me a list of all the differences between both pieces of code this is important
roelof
roelofOP15mo ago
and the function is called ob_end_flush instead of ob_flush
ἔρως
ἔρως15mo ago
oh, you got there! that's right, the function is different
roelof
roelofOP15mo ago
changed it
ἔρως
ἔρως15mo ago
but, you can't return the value, which means that ... ?
roelof
roelofOP15mo ago
I have to put it into a variable
ἔρως
ἔρως15mo ago
exactly, and give it to the email function
roelof
roelofOP15mo ago
which is the question I asked a few minutes ago How to do that
ἔρως
ἔρως15mo ago
replace return with the variable
roelof
roelofOP15mo ago
did it already but still do not see the error mesage that the mail has failed I see the whole email and still at the submit.php file
ἔρως
ἔρως15mo ago
what do you have now?
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
can you send the code?
roelof
roelofOP15mo ago
of the whole file of course
ἔρως
ἔρως15mo ago
no just the part around the email function
roelof
roelofOP15mo ago
ob_start();
load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data
]);
$template = ob_end_flush();


$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'],$template);

if (!$mail_send) {
wp_send_json(["Error", "Mail cannot be send"]);
}
ob_start();
load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data
]);
$template = ob_end_flush();


$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'],$template);

if (!$mail_send) {
wp_send_json(["Error", "Mail cannot be send"]);
}
I will be away for the rest of the evening See your remarks tomorrow
ἔρως
ἔρως15mo ago
that should be it wp_send_json(["Error", "Mail cannot be send"]); <-- you should send an error
roelof
roelofOP15mo ago
nope, still see only the form on submit.php here locally and the site still stays on the submit.php url 😢 wierd When I look at the email send I see this :
Date: Sat, 24 Feb 2024 19:39:08 +0000
From: WordPress <[email protected]>
Message-ID: <[email protected]>
X-Mailer: PHPMailer 6.8.1 (https://github.com/PHPMailer/PHPMailer)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8

1
Date: Sat, 24 Feb 2024 19:39:08 +0000
From: WordPress <[email protected]>
Message-ID: <[email protected]>
X-Mailer: PHPMailer 6.8.1 (https://github.com/PHPMailer/PHPMailer)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8

1
so the email variable is not used and the message is gone I think we have to change the content-type to text/html somehow
ἔρως
ἔρως15mo ago
weird 🤔 https://developer.wordpress.org/reference/functions/wp_mail/ i made a mistake ob_end_flush must be ob_get_clean instead
roelof
roelofOP15mo ago
oke right now I see a white page on the submit url
ἔρως
ἔρως15mo ago
good as you should except an error message
roelof
roelofOP15mo ago
When I do this :
$headers = array('Content-Type: text/html; charset=UTF-8');
$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'],$template, $headers);
$headers = array('Content-Type: text/html; charset=UTF-8');
$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'],$template, $headers);
the email looks good to me oke, but how do I get my form back so I can show the messages
ἔρως
ἔρως15mo ago
you don't you send the email over the fetch
roelof
roelofOP15mo ago
bummer so the user never sees the message if the email is send or not not happy about that
ἔρως
ἔρως15mo ago
that's the job of the javascript and json
roelof
roelofOP15mo ago
oke but as the user do not see the messages the json has no meaning one of the requirements was that the user sees the form and above it the messges
ἔρως
ἔρως15mo ago
that's what the javascript does you don't do it from the other file
roelof
roelofOP15mo ago
I understand but right now after the form is send the empty submit page is shown and not the messages if the email is send or not And the requirements said I have to do that
ἔρως
ἔρως15mo ago
if the form is submitted, it should show the errors sent from json, or a success message in javascript
roelof
roelofOP15mo ago
like I said that is not what the requirements of the challenge is THere is a stated that the messages are shown above the form I told you that several times
ἔρως
ἔρως15mo ago
then implement it you received the message in javascript you have half implemented already
roelof
roelofOP15mo ago
That is why I want to use a redirect to the form with wp_redirect($atts['refer'] )
ἔρως
ἔρως15mo ago
that's not going to work
roelof
roelofOP15mo ago
but you told me not to do that
ἔρως
ἔρως15mo ago
i told you not to do that because that wont work
roelof
roelofOP15mo ago
oke then I have to think how to make it work maybe again use the load-template thing ?
ἔρως
ἔρως15mo ago
no, you do it in javascript
roelof
roelofOP15mo ago
oke, then I have to think how I know the form is in form.php or in the page where the form is used
ἔρως
ἔρως15mo ago
did you checked the json you get?
roelof
roelofOP15mo ago
yep on the answer tab no data on the network thing on the answer tab
ἔρως
ἔρως15mo ago
nothing at all?
roelof
roelofOP15mo ago
nothing at all
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
you probably have an error somewhere vscode should tell you where
roelof
roelofOP15mo ago
also nothing to find
No description
ἔρως
ἔρως15mo ago
in that submit file, by the way
roelof
roelofOP15mo ago
no red lines
ἔρως
ἔρως15mo ago
you didnt enable the wordpress stubs
roelof
roelofOP15mo ago
I did as far as I know
ἔρως
ἔρως15mo ago
check it
roelof
roelofOP15mo ago
require_once($path . "wp-load.php");
require_once($path . "wp-load.php");
ἔρως
ἔρως15mo ago
that's not the wordpress stubs it's a vscode setting
roelof
roelofOP15mo ago
or do you mean the plugin
ἔρως
ἔρως15mo ago
yes
roelof
roelofOP15mo ago
i installed this ones :
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
why?
roelof
roelofOP15mo ago
because I thought and you told me to use them
ἔρως
ἔρως15mo ago
do you have intellephense installed?
roelof
roelofOP15mo ago
yes
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
no, that's some random plugin
roelof
roelofOP15mo ago
oke which one do you then mean ?
roelof
roelofOP15mo ago
oke installed it
ἔρως
ἔρως15mo ago
uninstall all the other random stuff
roelof
roelofOP15mo ago
oke no problems found
ἔρως
ἔρως15mo ago
now, enable the wordpress stubs go to the plugin settings you should have a huge list of items with names at the end, click "add item" and type wordpress
roelof
roelofOP15mo ago
you mean in this page ?
ἔρως
ἔρως15mo ago
no, in the settings vscode settings
roelof
roelofOP15mo ago
it was already enabled
roelof
roelofOP15mo ago
No description
roelof
roelofOP15mo ago
and still no problems found
ἔρως
ἔρως15mo ago
and the functions now are showing without yellow lines?
roelof
roelofOP15mo ago
sorry, yes I told you some minutes ago that there were no probems found and there the yellow lines disappear
ἔρως
ἔρως15mo ago
good something is weird then
roelof
roelofOP15mo ago
and the 3tab is still empty yep again all the code is pushed
ἔρως
ἔρως15mo ago
i will check in a bit
roelof
roelofOP15mo ago
oke take your time here almost time to sleep
ἔρως
ἔρως15mo ago
take a break
roelof
roelofOP15mo ago
I will I cannot do anything at the moment have no clue where to look
ἔρως
ἔρως15mo ago
same, but im playing overwatch at the same time
roelof
roelofOP15mo ago
oke I have nothing wth games but if you enjy it , much joy with it
ἔρως
ἔρως15mo ago
try commenting out the output buffers
roelof
roelofOP15mo ago
he when I do it like this :
$headers = array('Content-Type: text/html; charset=UTF-8');
$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'],load_template(__DIR__ . '/templates/email.php', false, [
' data' => $data
]), $headers);
$headers = array('Content-Type: text/html; charset=UTF-8');
$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'],load_template(__DIR__ . '/templates/email.php', false, [
' data' => $data
]), $headers);
then I see again a message that data is unknown here :
$data = array_replace(["class" => "", "subject" => "", "email" => "", "message" => ""], $args['data']);
$data = array_replace(["class" => "", "subject" => "", "email" => "", "message" => ""], $args['data']);
this one :
Fatal error: Uncaught TypeError: array_replace(): Argument #2 must be of type array, null given in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php:5 Stack trace: #0 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php(5): array_replace(Array, NULL) #1 C:\laragon\www\myBlog\wp-includes\template.php(792): require('C:\\laragon\\www\\...') #2 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php(40): load_template('C:\\laragon\\www\\...', false, Array) #3 {main} thrown in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php on line 5
Fatal error: Uncaught TypeError: array_replace(): Argument #2 must be of type array, null given in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php:5 Stack trace: #0 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php(5): array_replace(Array, NULL) #1 C:\laragon\www\myBlog\wp-includes\template.php(792): require('C:\\laragon\\www\\...') #2 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php(40): load_template('C:\\laragon\\www\\...', false, Array) #3 {main} thrown in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php on line 5
I thought we solved that one code seems to be very fragile and I think that is not good
ἔρως
ἔρως15mo ago
add a var_dump() before it dump the $args variable
roelof
roelofOP15mo ago
array(1) { [" data"]=> array(5) { ["subject"]=> string(5) "test " ["email"]=> string(19) "[email protected]" ["message"]=> string(5) " test" ["_wpnonce"]=> string(10) "1f35d755fa" ["_wp_http_referer"]=> string(1) "/" } }
array(1) { [" data"]=> array(5) { ["subject"]=> string(5) "test " ["email"]=> string(19) "[email protected]" ["message"]=> string(5) " test" ["_wpnonce"]=> string(10) "1f35d755fa" ["_wp_http_referer"]=> string(1) "/" } }
ἔρως
ἔρως15mo ago
you misspelled the key somewhere
roelof
roelofOP15mo ago
seems well to me
ἔρως
ἔρως15mo ago
can you take a screenshot?
roelof
roelofOP15mo ago
No description
ἔρως
ἔρως15mo ago
not there that's bad that it does that, but that's not where you're supposed to see it
roelof
roelofOP15mo ago
I agree the error messages are very very cryptic
ἔρως
ἔρως15mo ago
i disagree you're doing it wrong that's why the variable doesn't exist
roelof
roelofOP15mo ago
???
ἔρως
ἔρως15mo ago
if(!defined('ABSPATH')) die();
if(!defined('ABSPATH')) die();
add this to the top
roelof
roelofOP15mo ago
same problem when I add this to the top of the mycustomForm.php
ἔρως
ἔρως15mo ago
oh, wait, you're doing it kinda right add (array) before $_POST
roelof
roelofOP15mo ago
oke I have to look again where I used that
ἔρως
ἔρως15mo ago
and then, you need to throw an error when it isn't a post request
roelof
roelofOP15mo ago
oke where ? because I still see the same annoying error
ἔρως
ἔρως15mo ago
right at the top of the submit file
roelof
roelofOP15mo ago
oke, I will tr y it tomrrow feel very tired
ἔρως
ἔρως15mo ago
the problem is that you're trying to access $_POST when you're doing a GET request
roelof
roelofOP15mo ago
your sure
ἔρως
ἔρως15mo ago
yes
roelof
roelofOP15mo ago
If I change $POST to GET I get a lot of validation errrors
roelof
roelofOP15mo ago
No description
roelof
roelofOP15mo ago
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) $_GET);
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) $_GET);
ἔρως
ἔρως15mo ago
because you're doing it wrong keep it at $_POST
roelof
roelofOP15mo ago
Good morning how do you mean this then ?
the problem is that you're trying to access $_POST when you're doing a GET request
the problem is that you're trying to access $_POST when you're doing a GET request
ἔρως
ἔρως15mo ago
when you access a link in the browser, it's a GET request
roelof
roelofOP15mo ago
yes So in the same function we did the post we also have to write a get fetch ? nope, I get now a 500 error when I do this:
async function send_to_backend(data, url) {
await fetch( url, {
method: 'POST',
body: data
})
var data = await fetch(url);
var response = await data;
console.log(response);
}
async function send_to_backend(data, url) {
await fetch( url, {
method: 'POST',
body: data
})
var data = await fetch(url);
var response = await data;
console.log(response);
}
and still a error message that data is not existing which makes totally no sense and this gives me also a 200 error :
async function send_to_backend(data, url) {
await fetch( url, {
method: 'POST',
body: data
})
}

async function fetch_from_backend(url) {
var data = await fetch(url);
var response = await data;
console.log(response);
};
async function send_to_backend(data, url) {
await fetch( url, {
method: 'POST',
body: data
})
}

async function fetch_from_backend(url) {
var data = await fetch(url);
var response = await data;
console.log(response);
};
send_to_backend(formdata, form.action);
var backend_rseponse = fetch_from_backend(form.action);
console.log(backend_response);
send_to_backend(formdata, form.action);
var backend_rseponse = fetch_from_backend(form.action);
console.log(backend_response);
but no fetch on the network tab is the wp_send _json not wrong ? if I read it , it sending back to a ajax request but as far as I know we are not using ajax I try to find out why I do not see a get request in m y network tab any hints
ἔρως
ἔρως14mo ago
did you push to your gitlab?
roelof
roelofOP14mo ago
oops. I forgot last version is now pushed
ἔρως
ἔρως14mo ago
this is wrong you're sending a post request then you do nothing with the result and then, you send another get request
roelof
roelofOP14mo ago
oke, how do we do it then
ἔρως
ἔρως14mo ago
1 single fetch - the send_to_backend
roelof
roelofOP14mo ago
im still try to figure out what you mean with this line :
the problem is that you're trying to access $_POST when you're doing a GET request
the problem is that you're trying to access $_POST when you're doing a GET request
ἔρως
ἔρως14mo ago
that was when you accessed submit.php in the browser not from the code what you do in the code won't be what you do by accessing the url in the browser
roelof
roelofOP14mo ago
oke then there I make a thinking error
ἔρως
ἔρως14mo ago
more like a misunderstanding
roelof
roelofOP14mo ago
oke, so now I have to figure out how and where to make the redirect so I can show the output in the form
ἔρως
ἔρως14mo ago
you don't make any redirects that's why you wrote everything in javascript
roelof
roelofOP14mo ago
yep. I see it now I still see the form and not what I had that the browser stuck at submit.php
ἔρως
ἔρως14mo ago
what do you mean?
roelof
roelofOP14mo ago
chips. now I get a 500 error back on submit.php and this error :
Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
damned again this annoying error :
Fatal error: Uncaught TypeError: array_replace(): Argument #2 must be of type array, null given in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php:7 Stack trace: #0 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php(7): array_replace(Array, NULL) #1 C:\laragon\www\myBlog\wp-includes\template.php(792): require('C:\\laragon\\www\\...') #2 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php(34): load_template('C:\\laragon\\www\\...', false, Array) #3 {main} thrown in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php on line 7
Fatal error: Uncaught TypeError: array_replace(): Argument #2 must be of type array, null given in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php:7 Stack trace: #0 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php(7): array_replace(Array, NULL) #1 C:\laragon\www\myBlog\wp-includes\template.php(792): require('C:\\laragon\\www\\...') #2 C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\submit.php(34): load_template('C:\\laragon\\www\\...', false, Array) #3 {main} thrown in C:\laragon\www\myBlog\wp-content\plugins\mycustomForm\templates\email.php on line 7
ἔρως
ἔρως14mo ago
you didn't do what i told you to do with the submit.php file
roelof
roelofOP14mo ago
sometimes I seems to be gone and then it pops out again What do I forgot then ?
ἔρως
ἔρως14mo ago
task for you to do: - detect if it isn't a POST request - create a wp_error with an http 400 error code - send the error over json this, at the top of the submit.php file
roelof
roelofOP14mo ago
so like this :
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
$error = new WP_Error() ;
$error->$add(400, "this is not a post request");
wp_send_json_error($error);
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
$error = new WP_Error() ;
$error->$add(400, "this is not a post request");
wp_send_json_error($error);
}
`
ἔρως
ἔρως14mo ago
instead of adding an error, you can set it in the constructor
roelof
roelofOP14mo ago
oke
ἔρως
ἔρως14mo ago
but yes, that should work
roelof
roelofOP14mo ago
you mean like this :

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
$error = new WP_Error(400, "bad request") ;
wp_send_json_error($error);
}

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
$error = new WP_Error(400, "bad request") ;
wp_send_json_error($error);
}
ἔρως
ἔρως14mo ago
"this is not a post request" <-- instead of this, just say "Bad Request" also, it's 400, not 499
roelof
roelofOP14mo ago
canged
ἔρως
ἔρως14mo ago
and now, what do you get?
roelof
roelofOP14mo ago
only a get request
ἔρως
ἔρως14mo ago
🤔 but the function does a post request that's literally impossible
roelof
roelofOP14mo ago
and as answer the form as html
ἔρως
ἔρως14mo ago
show me
roelof
roelofOP14mo ago
No description
roelof
roelofOP14mo ago
No description
ἔρως
ἔρως14mo ago
that's the wrong url but the initial is "document" 🤔 did you submit the form?
roelof
roelofOP14mo ago
yep
ἔρως
ἔρως14mo ago
you sure?
roelof
roelofOP14mo ago
oke, did I again and see now the 500 back not funny
ἔρως
ἔρως14mo ago
what's the error?
roelof
roelofOP14mo ago
same stuipid error as before
ἔρως
ἔρως14mo ago
that's literally impossible can you push the code?
roelof
roelofOP14mo ago
and this is the beginning of the submit.php code
<?php



$path = preg_replace('/wp-content.*$/', '', __DIR__);
require_once($path . "wp-load.php");

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
$error = new WP_Error(400, "Bad request") ;
wp_send_json_error($error);
}

$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) $_POST);
<?php



$path = preg_replace('/wp-content.*$/', '', __DIR__);
require_once($path . "wp-load.php");

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
$error = new WP_Error(400, "Bad request") ;
wp_send_json_error($error);
}

$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) $_POST);
of course psuhed
ἔρως
ἔρως14mo ago
i will test this in a local server
roelof
roelofOP14mo ago
oke I have to leave because my daugther needs to go to sport so from 16.30 till 18,30 im not behind the computer
ἔρως
ἔρως14mo ago
that's alright i found one big issue you're still using ob_end_flush
roelof
roelofOP14mo ago
?? where then I cannot find it on subnit.php
ἔρως
ἔρως14mo ago
in the plugin file
roelof
roelofOP14mo ago
oke
roelof
roelofOP14mo ago
oke, so it has to be this :
function roelof_add_custom_shortcode()
{
function roelof_contact_form($atts, $content, $shortcode_tag)
{
static $counter = 0;
$counter ++ ;
wp_enqueue_style( 'rw-custom-form-style' );
wp_enqueue_script( 'rw-custom-form-script' );
// ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode' => $shortcode_tag,
'prefix' => $shortcode_tag . $counter,
]);
// return ob_end_flush();
}

add_shortcode('contact_form', 'roelof_contact_form');
}
function roelof_add_custom_shortcode()
{
function roelof_contact_form($atts, $content, $shortcode_tag)
{
static $counter = 0;
$counter ++ ;
wp_enqueue_style( 'rw-custom-form-style' );
wp_enqueue_script( 'rw-custom-form-script' );
// ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode' => $shortcode_tag,
'prefix' => $shortcode_tag . $counter,
]);
// return ob_end_flush();
}

add_shortcode('contact_form', 'roelof_contact_form');
}
if so , still the same 500 error
ἔρως
ἔρως14mo ago
// return ob_end_flush(); <-- this is wrong as well you have to replace ob_end_flush with ob_get_clean
roelof
roelofOP14mo ago
oke, but you told me a few minutes ago to comment that out
ἔρως
ἔρως14mo ago
no, i just said it is wrong
roelof
roelofOP14mo ago
oke changed it but still that annoying 500 error
ἔρως
ἔρως14mo ago
that's fine, can you push?
roelof
roelofOP14mo ago
I did and I have to leave as I said\
ἔρως
ἔρως14mo ago
that's fine
roelof
roelofOP14mo ago
I will read your coments after it
ἔρως
ἔρως14mo ago
well, the form doesn't work at all
ἔρως
ἔρως14mo ago
there's no event listener here
No description
ἔρως
ἔρως14mo ago
the url is misspelled
No description
ἔρως
ἔρως14mo ago
after fixing the name, it's working
No description
ἔρως
ἔρως14mo ago
you have a bug: even valid emails are invalid
ἔρως
ἔρως14mo ago
No description
roelof
roelofOP14mo ago
Wierd on the frontend i see the validatie errors
ἔρως
ἔρως14mo ago
yes, but it's always invalid wait, i see the problem
ἔρως
ἔρως14mo ago
the default value contains a space!
No description
roelof
roelofOP14mo ago
Oké i used [email protected] and that worked Oké did not noticed that
ἔρως
ἔρως14mo ago
the validation work, if you delete the space that shouldn't be there https://gitlab.com/roelofwobben/short-code-form/-/blob/main/templates/form.php?ref_type=heads#L15
roelof
roelofOP14mo ago
Will charge that woensdag i home again
ἔρως
ἔρως14mo ago
value="<?= esc_attr($atts['subject']) ?> " the subject, email and message have the same issue https://gitlab.com/roelofwobben/short-code-form/-/blob/main/submit.php?ref_type=heads#L41 <-- you have a space here
array(1) {
[" data"]=>
array(5) {
["subject"]=>
string(5) "test "
["email"]=>
string(7) "[email protected]"
["message"]=>
string(5) "test "
["_wpnonce"]=>
string(10) "d37c254d7d"
["_wp_http_referer"]=>
string(36) "/scope:0.0910988219889797/?page_id=2"
}
}
array(1) {
[" data"]=>
array(5) {
["subject"]=>
string(5) "test "
["email"]=>
string(7) "[email protected]"
["message"]=>
string(5) "test "
["_wpnonce"]=>
string(10) "d37c254d7d"
["_wp_http_referer"]=>
string(36) "/scope:0.0910988219889797/?page_id=2"
}
}
which causes this weird 💩 in the output and then you get this, which is correct:
<br />
<b>Warning</b>: Undefined array key "data" in <b>/wordpress/wp-content/plugins/short-code-form-main/templates/email.php</b> on line <b>7</b><br />
<br />
<b>Fatal error</b>: Uncaught TypeError: array_replace(): Argument #2 must be of type array, null given in /wordpress/wp-content/plugins/short-code-form-main/templates/email.php:7
Stack trace:
#0 /wordpress/wp-content/plugins/short-code-form-main/templates/email.php(7): array_replace(Array, NULL)
#1 /wordpress/wp-includes/template.php(2): require('/wordpress/wp-c...')
#2 /wordpress/wp-content/plugins/short-code-form-main/submit.php(40): load_template('/wordpress/wp-c...', false, Array)
#3 {main}
thrown in <b>/wordpress/wp-content/plugins/short-code-form-main/templates/email.php</b> on line <b>7</b><br />
<br />
<b>Warning</b>: Undefined array key "data" in <b>/wordpress/wp-content/plugins/short-code-form-main/templates/email.php</b> on line <b>7</b><br />
<br />
<b>Fatal error</b>: Uncaught TypeError: array_replace(): Argument #2 must be of type array, null given in /wordpress/wp-content/plugins/short-code-form-main/templates/email.php:7
Stack trace:
#0 /wordpress/wp-content/plugins/short-code-form-main/templates/email.php(7): array_replace(Array, NULL)
#1 /wordpress/wp-includes/template.php(2): require('/wordpress/wp-c...')
#2 /wordpress/wp-content/plugins/short-code-form-main/submit.php(40): load_template('/wordpress/wp-c...', false, Array)
#3 {main}
thrown in <b>/wordpress/wp-content/plugins/short-code-form-main/templates/email.php</b> on line <b>7</b><br />
because "data" doesn't exist - you wrote " data"
roelof
roelofOP14mo ago
Oké so all mu problems are space related
ἔρως
ἔρως14mo ago
yes if you use spaces to indent, start using the tab key
roelof
roelofOP14mo ago
Lot of workshop to to thuis evenweg then
ἔρως
ἔρως14mo ago
just small stuff, don't worry
roelof
roelofOP14mo ago
Evening stupid phone
ἔρως
ἔρως14mo ago
keyboards on phones suck
roelof
roelofOP14mo ago
Yep and you have a sort of spelling control and that sucks more
ἔρως
ἔρως14mo ago
i disable that
roelof
roelofOP14mo ago
I have not found out how
ἔρως
ἔρως14mo ago
is it an iphone?
roelof
roelofOP14mo ago
Do you work with wordpress for work No android
ἔρως
ἔρως14mo ago
i do, yes
roelof
roelofOP14mo ago
Oke
ἔρως
ἔρως14mo ago
you have to get into the settings of your keyboard
roelof
roelofOP14mo ago
Front ot back or full stack?
ἔρως
ἔρως14mo ago
full stack
roelof
roelofOP14mo ago
Woud be a Dreams of me Bit i thinl om too old to grt a wordpress job snd mu problems causes that i can only work for 8 hours a week
ἔρως
ἔρως14mo ago
8 hours a week is very little for any job
roelof
roelofOP14mo ago
Exactly
ἔρως
ἔρως14mo ago
but it isn't very little for personal projects if you make something that generates money for you, you don't have to worry
roelof
roelofOP14mo ago
So i think it would be a nicehoo y Nice hobby
ἔρως
ἔρως14mo ago
it would
roelof
roelofOP14mo ago
We see what the future Will bring
ἔρως
ἔρως14mo ago
yes, because you need to practice first 90% of your project is basic html, css, javascript and php
roelof
roelofOP14mo ago
Practice a lot
ἔρως
ἔρως14mo ago
the other 10% is wordpress specific and on those 90% is where all your troubles are at if you know how to program, you can work on php by your own
roelof
roelofOP14mo ago
Oké i practice now for 2 tears with html css snd js
ἔρως
ἔρως14mo ago
it's the mindset to grab previous solutions to previous problems and apply them on different situations for example, you had to grab the output of the form template and use it but you struggled to do the same for the email template even if the solution is 99.9% the exact same but this will come from practice
roelof
roelofOP14mo ago
Yep a problemen i know
ἔρως
ἔρως14mo ago
it is, but the gears will fit when you have to solve the same problem more and more often
roelof
roelofOP14mo ago
I hope
ἔρως
ἔρως14mo ago
it will, you need practice
roelof
roelofOP14mo ago
We see
ἔρως
ἔρως14mo ago
practice would also help you find these issues i just pointed out to you
roelof
roelofOP14mo ago
In 1 or 2 weeks i Will get a New chalkenge
ἔρως
ἔρως14mo ago
just worry about finishing this one
roelof
roelofOP14mo ago
Every 1 week if a month i get a New challenge I not worrying. One problem at the time
ἔρως
ἔρως14mo ago
exactly you can do this
roelof
roelofOP14mo ago
I hope you are richt. It do not feel like that at the moment I doubt if developing is for me
ἔρως
ἔρως14mo ago
that's something only you can answer
roelof
roelofOP14mo ago
I know Maybe i want too fast. Another working point for me
ἔρως
ἔρως14mo ago
everybody wants to see results now
roelof
roelofOP14mo ago
oke, now a 200 but still the form that is send back and not the json
ἔρως
ἔρως14mo ago
push it
roelof
roelofOP14mo ago
No description
ἔρως
ἔρως14mo ago
did you remove the var dumps?
roelof
roelofOP14mo ago
nope, I found one at submit.php
ἔρως
ἔρως14mo ago
you will find 2 dont search manually use the vscode search functionality
roelof
roelofOP14mo ago
oke, both are deleted same problem it still seems the form is send back instead of the json which is wierd with a get request
ἔρως
ἔρως14mo ago
how are you testing this?
roelof
roelofOP14mo ago
fill in the form on the site and submit it
ἔρως
ἔρως14mo ago
did you fix the javascript?
roelof
roelofOP14mo ago
as far as I know I did fix the javascript to the orginal
ἔρως
ἔρως14mo ago
did you fixed the path?
roelof
roelofOP14mo ago
in the form ?
ἔρως
ἔρως14mo ago
the javascript file path
roelof
roelofOP14mo ago
have to look back where and what was wrong moment
ἔρως
ἔρως14mo ago
take your time
roelof
roelofOP14mo ago
the url is now : <form method="post" action="http://myblog.test/wp-content/plugins/mycustomForm/submit.php"> which looks well to me
ἔρως
ἔρως14mo ago
the javascript url
roelof
roelofOP14mo ago
looks also well to me :
<script src="http://myblog.test/wp-content/plugins/mycustomForm/js/mycustomForm.js?ver=1.0.0" id="rw-custom-form-script-js"></script>
<script src="http://myblog.test/wp-content/plugins/mycustomForm/js/mycustomForm.js?ver=1.0.0" id="rw-custom-form-script-js"></script>
sorry. no idea what you mean with that
ἔρως
ἔρως14mo ago
it's actually wrong look at the name of the javascript file and compare the name in that path
roelof
roelofOP14mo ago
chips, stupid details but wierd that I saw the front end validation error that file provides that hmm, still the get gives back the form
roelof
roelofOP14mo ago
and stilll this annoying error : `
No description
roelof
roelofOP14mo ago
Any idea what causes that json error and why the form is send back instead of the json Why can a Get request give back a form makes totally no sense to me
ἔρως
ἔρως14mo ago
did you push it?
roelof
roelofOP14mo ago
yes
ἔρως
ἔρως14mo ago
the message still has the space i see the problem
ἔρως
ἔρως14mo ago
this is the problem
No description
ἔρως
ἔρως14mo ago
https://gitlab.com/roelofwobben/short-code-form/-/blob/main/submit.php?ref_type=heads#L38 you're not capturing the output of the template and you removed the $headers variable and on line 44, you're not sending a wp_error object
roelof
roelofOP14mo ago
moment, with message you mean here :
<textarea id="<?= $prefix ?>_message" name ="message" required value="<?= esc_attr($atts['message']) ?>"> </textarea>
<textarea id="<?= $prefix ?>_message" name ="message" required value="<?= esc_attr($atts['message']) ?>"> </textarea>
ἔρως
ἔρως14mo ago
textarea doesn't have a value field you put the content inside the tags
roelof
roelofOP14mo ago
and line 44 needs to be like this :
wp_send_json(new WP_Error("Error", "Mail cannot be send"));
wp_send_json(new WP_Error("Error", "Mail cannot be send"));
ἔρως
ἔρως14mo ago
yes, for now, that's enough
roelof
roelofOP14mo ago
oke and the text area has to look like this :
<textarea id="<?= $prefix ?>_message" name ="message" required > <?= esc_attr($atts['message']) ?> </textarea>
<textarea id="<?= $prefix ?>_message" name ="message" required > <?= esc_attr($atts['message']) ?> </textarea>
ἔρως
ἔρως14mo ago
no spaces! let me fix it for you <textarea id="<?= $prefix ?>_message" name="message" required><?= esc_attr($atts['message']) ?></textarea>
roelof
roelofOP14mo ago
still the annoying json error
roelof
roelofOP14mo ago
No description
roelof
roelofOP14mo ago
and the network tab is totally empty now
ἔρως
ἔρως14mo ago
push it
roelof
roelofOP14mo ago
pushed
ἔρως
ἔρως14mo ago
give me a bit
roelof
roelofOP14mo ago
and now no front end validation errors anymore take all the time you need
ἔρως
ἔρως14mo ago
have you tried to use https://playground.wordpress.net/?
roelof
roelofOP14mo ago
no never knew it existing hmm, I have to look then how to store changes now after a page refresh everything seems to be gone and how I can edit then my plugin
ἔρως
ἔρως14mo ago
you can change it to store in the browser
roelof
roelofOP14mo ago
yep but then you cannot see what I do so it looks not a good colleberation tool
ἔρως
ἔρως14mo ago
the idea is to use a different server on a different wordpress install
roelof
roelofOP14mo ago
oke
ἔρως
ἔρως14mo ago
and with this, i also don't have to use xampp to test your stuff, so, a win-win for me
roelof
roelofOP14mo ago
still do not see how I can help me to make this very annoying challenge work
ἔρως
ἔρως14mo ago
your plugin does have a very bad issue running it with no arguments just crashes the site
roelof
roelofOP14mo ago
oops. the front end validation schould fetch that one but since I changed the name of the js file the front end validation stop working 😦
ἔρως
ἔρως14mo ago
you didn't fix anything i told you to fix on the submit.php file
roelof
roelofOP14mo ago
I did not
ἔρως
ἔρως14mo ago
you did not
ἔρως
ἔρως14mo ago
No description
roelof
roelofOP14mo ago
then I think I totally misunderstood what you wanted im totally confused now
roelof
roelofOP14mo ago
I fairly think I did everything you asked for but that is not in submit,php
roelof
roelofOP14mo ago
yes I have the admin email and the email adress I want to send to and then the template that is as far as I know what you asked when I told me to get rid of the ob_start part
ἔρως
ἔρως14mo ago
but look at what you're doing to the template, in both files, and compare the differences i never told you that, because that's the wrong thing to do what i did say was that i told you the wrong name of the 2nd function, which i told you the correct function after
roelof
roelofOP14mo ago
oke, then I have to look that up totally confused now
ἔρως
ἔρως14mo ago
dude, simply do the same you did in that file, but instead of returning, you save in a variable and pass it to the email
roelof
roelofOP14mo ago
so like this :
$template = load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data]);

$headers = array('Content-Type: text/html; charset=UTF-8');
$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'],$template, false, [
'data' => $data
], $headers);
$template = load_template(__DIR__ . '/templates/email.php', false, [
'data' => $data]);

$headers = array('Content-Type: text/html; charset=UTF-8');
$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'],$template, false, [
'data' => $data
], $headers);
because yesterday you told me to get rid of the ob-stuff or do you want it back now ?
ἔρως
ἔρως14mo ago
look at how you're getting the output in the file i pointed to i never told you to get rid of it all i said was that i told you the wrong function and to replace the name of it
roelof
roelofOP14mo ago
yesterday you did, when we are looking why things were not working
ἔρως
ἔρως14mo ago
yeah, for debugging for just a short bit but it's fine, add it back
roelof
roelofOP14mo ago
oke I have to see how to place it back
ἔρως
ἔρως14mo ago
it's the same as the other file, but instead of returning you save in a variable
roelof
roelofOP14mo ago
still the same annoying json error when I do this :
ob_start();
load_template(__DIR__ . '/templates/email.php', false, [
' data' => $data
]);
$template = ob_get_clean();

$headers = array('Content-Type: text/html; charset=UTF-8');
$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'],$template, false, [
'data' => $data
], $headers);
ob_start();
load_template(__DIR__ . '/templates/email.php', false, [
' data' => $data
]);
$template = ob_get_clean();

$headers = array('Content-Type: text/html; charset=UTF-8');
$mail_send = wp_mail(get_option( 'admin_email' ), $data['email'],$template, false, [
'data' => $data
], $headers);
and the 500 error back
ἔρως
ἔρως14mo ago
did you push?
roelof
roelofOP14mo ago
yep
roelof
roelofOP14mo ago
as far as I understand it schould send the email and so on to the email form file
ἔρως
ἔρως14mo ago
https://developer.wordpress.org/reference/functions/wp_mail/ wp_mail( string|string[] $to, string $subject, string $message, string|string[] $headers = ”, string|string[] $attachments = array() ): bool this is what the function needs
roelof
roelofOP14mo ago
oke I think some pieces of the load-template were left behind the 500 error is gone but the json error stays 😦
ἔρως
ἔρως14mo ago
yup, you left the array behind, re-created the call and you left the same bug did you push?
roelof
roelofOP14mo ago
also solved I had to add a suceess message
wp_send_json(new WP_Error(200, "Mail is send"));
wp_send_json(new WP_Error(200, "Mail is send"));
ἔρως
ἔρως14mo ago
there's a slightly better way, but that's a nice hack
roelof
roelofOP14mo ago
this still do not look well to me
roelof
roelofOP14mo ago
No description
roelof
roelofOP14mo ago
I know it is a hack but had to try something 🙂
ἔρως
ἔρως14mo ago
that's just a page load
roelof
roelofOP14mo ago
oke because this looks well to me :
Object { errors: {…}, error_data: [] }

error_data: Array []

errors: Object { 200: (1) […] }
​​
200: Array [ "Mail is send" ]
​​
<prototype>: Object { … }

<prototype>: Object { … }
myCustomForm.js:42:19


Object { errors: {…}, error_data: [] }

error_data: Array []

errors: Object { 200: (1) […] }
​​
200: Array [ "Mail is send" ]
​​
<prototype>: Object { … }

<prototype>: Object { … }
myCustomForm.js:42:19


ἔρως
ἔρως14mo ago
it does
roelof
roelofOP14mo ago
What is then a better way ?
ἔρως
ἔρως14mo ago
wp_send_json(['success' => true])
roelof
roelofOP14mo ago
thanks on the form I see now these errors on the console
Er kan niet op het ongeldige formulierbesturingselement met name=‘_wpnonce’ worden gefocust.
Er kan niet op het ongeldige formulierbesturingselement met name=‘_wpnonce’ worden gefocust.
in Engissh I cannot be a focus on a non valid element with as name _wponce
ἔρως
ἔρως14mo ago
weird that the browser is doing that move the nonce field to the end of the form, instead of the top
roelof
roelofOP14mo ago
oke I see the same error on _wp_http_refer Try to find out why the front end validation does not work suddenly
ἔρως
ἔρως14mo ago
did you push?
roelof
roelofOP14mo ago
did it right now seems to me that the js file is not used at all and that only the submit is used
ἔρως
ἔρως14mo ago
no, you're just missing the last step handing the response from the server
roelof
roelofOP14mo ago
I know but that should not influence the front end error messages shown of the front end validation messages
ἔρως
ἔρως14mo ago
it should you literally don't have that part implemented
ἔρως
ἔρως14mo ago
and where do you use it?
ἔρως
ἔρως14mo ago
is it before or after you send the request to the server?
roelof
roelofOP14mo ago
if its right before just before
ἔρως
ἔρως14mo ago
then the answer from the server isn't being handled yet
roelof
roelofOP14mo ago
if (error_messages.length) {
showErrorMessages(error_messages, form);
return
}

var backend_response = send_to_backend(formdata, form.action);
})
if (error_messages.length) {
showErrorMessages(error_messages, form);
return
}

var backend_response = send_to_backend(formdata, form.action);
})
ἔρως
ἔρως14mo ago
which means, you didn't implement the last step
roelof
roelofOP14mo ago
??
ἔρως
ἔρως14mo ago
you're in the last step: handle the response from the server
roelof
roelofOP14mo ago
oke
ἔρως
ἔρως14mo ago
if it is a success, show a success if it is an error, show errors
roelof
roelofOP14mo ago
oke so the part of show_errors in the js file I can delete that part is never send back to anything
ἔρως
ἔρως14mo ago
which part?
ἔρως
ἔρως14mo ago
that's the client-side errors you're going to be handling the server-side errors
roelof
roelofOP14mo ago
yep, and now they are never shown
ἔρως
ἔρως14mo ago
they are
roelof
roelofOP14mo ago
and I try to find out why
ἔρως
ἔρως14mo ago
set the message field to 1 character and the error shows
roelof
roelofOP14mo ago
then I see this :
No description
roelof
roelofOP14mo ago
and not the errors I made
ἔρως
ἔρως14mo ago
that's the html5 validation
roelof
roelofOP14mo ago
exactly
ἔρως
ἔρως14mo ago
if it has the required attribute, it shows that
roelof
roelofOP14mo ago
at a wierd way I do not understand the client validations are never shown
ἔρως
ἔρως14mo ago
because the form wasnt submitted
roelof
roelofOP14mo ago
?? and why did I work before we implemnt the server validation
ἔρως
ἔρως14mo ago
one isn't related to the other it works just put 1 character in the name, a valid email and 1 character in the message
roelof
roelofOP14mo ago
then it works when you make everything empty it does not work wierd
ἔρως
ἔρως14mo ago
it works but it's the browser doing it, instead of the javascript
roelof
roelofOP14mo ago
oke my idea was to make it work that if a user makes a invalid entry the front end would catch it now nothing happens not even a server validation error when a user entered a empty form I have to think well how to solve this one
ἔρως
ἔρως14mo ago
remove the required
roelof
roelofOP14mo ago
then it works well
roelof
roelofOP14mo ago
No description
roelof
roelofOP14mo ago
oke, then now add the stuff to catch the server validation errors then look if I really need all the css I have and make nice css and html to show the errrors and then finally done agree ? Thinking of making a sort of toaster or whatever it is called for every error of success message and im happy I do not see any server validation stuff 🙂 after 3 days frustating work this part finally works and I have to look how to get rid of the old error messages when submitting after making a error that will be tomorrow and after that stuff
ἔρως
ἔρως14mo ago
showing everything at the top is fine
roelof
roelofOP14mo ago
I kmow but if someone enters a non -valid email and after that enters a valid one the error mesage on the top stays visible and that is not good
ἔρως
ἔρως14mo ago
you can delete it on every submit
roelof
roelofOP14mo ago
that was I thought also but I cannot know if the error div is there it is now made when there is a submit and there is a error
function showErrorMessages(messages, form) {
// show the error messages

// Select the user-feedback div
const userFeedbackDiv = form.querySelector('.user_feedback');

// Create a new div to hold the error messages
const errorDiv = document.createElement('div');
errorDiv.classList.add('error');
function showErrorMessages(messages, form) {
// show the error messages

// Select the user-feedback div
const userFeedbackDiv = form.querySelector('.user_feedback');

// Create a new div to hold the error messages
const errorDiv = document.createElement('div');
errorDiv.classList.add('error');
will think about it tommorrow head is tired and I find it also wierd to add when there is no error yet got more and more the idea that this code is not right
// Select the user-feedback div
const userFeedbackDiv = form.querySelector('.user_feedback');

// Create a new div to hold the error messages
const errorDiv = document.createElement('div');
errorDiv.classList.add('error');

// Append the error div to the user-feedback div
userFeedbackDiv.appendChild(errorDiv);

// Select the error div
const errorDivElement = form.querySelector('.error');

// Clear any existing content in the error div
errorDivElement.innerHTML = '';
// Select the user-feedback div
const userFeedbackDiv = form.querySelector('.user_feedback');

// Create a new div to hold the error messages
const errorDiv = document.createElement('div');
errorDiv.classList.add('error');

// Append the error div to the user-feedback div
userFeedbackDiv.appendChild(errorDiv);

// Select the error div
const errorDivElement = form.querySelector('.error');

// Clear any existing content in the error div
errorDivElement.innerHTML = '';
why create a element when it can be already there but for now GN
ἔρως
ἔρως14mo ago
goodnight and rest we do it tomorrow
roelof
roelofOP14mo ago
oke Have a idea Could we not take care that the front end validation returns also json. Then we can have a seperate function for it because the front and back schould almost be the same
ἔρως
ἔρως14mo ago
or, just get the response from the server and shape it into what the frontend needs
roelof
roelofOP14mo ago
can also but as I say it will be then a lot of duplicate
ἔρως
ἔρως14mo ago
not if you dont send to the server the idea is super simple: - clear the errors/success - validate client side - has errors? show and exit - send to server - has errors? bash them until they fit the front-end error format, show and exit - no errors? show success thats the workflow you need for this
roelof
roelofOP14mo ago
ok4, then I have to think about point 1 and the last 2 points
ἔρως
ἔρως14mo ago
yup, but it is easier than you imagine
roelof
roelofOP14mo ago
oke, when im not so tired I will think how to solve point 1 I have to code but I think on the wrong place and some code is not right
ἔρως
ἔρως14mo ago
its fine, you can wait for me
roelof
roelofOP14mo ago
?? do you mean we can work together on this for example this evening ?
ἔρως
ἔρως14mo ago
after i leave from work, yes
roelof
roelofOP14mo ago
oke then im sure I understand what you mean
ἔρως
ἔρως14mo ago
just rest your mind for now
roelof
roelofOP14mo ago
I will getting proffesinioal help is hard work But what I do not see is how I can check if a div exist Otherwise I does not make sense to delete it
ἔρως
ἔρως14mo ago
you dont you can check if a div is in the body or if it has children
roelof
roelofOP14mo ago
have to think about the last part could be a solution
ἔρως
ἔρως14mo ago
it's a lot easier than you think
roelof
roelofOP14mo ago
yep., found out that you can do element.hasChildren so something like this :
const userFeedbackDiv = form.querySelector('.user_feedback');

if (userFeedbackDiv.hasChildren) {
userFeedbackDiv.innerHTML = ""
}

const userFeedbackDiv = form.querySelector('.user_feedback');

if (userFeedbackDiv.hasChildren) {
userFeedbackDiv.innerHTML = ""
}

ἔρως
ἔρως14mo ago
🤮 innerhtml dont do that please please please dont i would just hide the div then remove all the children
roelof
roelofOP14mo ago
Sorry What can i then use beter? Oké have to look how to remove the children
ἔρως
ἔρως14mo ago
while(element.firstChild) {
element.removeChild(element.firstChild);
}
while(element.firstChild) {
element.removeChild(element.firstChild);
}
or something like that
roelof
roelofOP14mo ago
oke then I would be something like this :
const userFeedbackDiv = form.querySelector('.user_feedback');

while (userFeedbackDiv.firstChild) {
userFeedBackDiv.removeChild(userFeedbackDiv.firstChild);
}

const userFeedbackDiv = form.querySelector('.user_feedback');

while (userFeedbackDiv.firstChild) {
userFeedBackDiv.removeChild(userFeedbackDiv.firstChild);
}

ἔρως
ἔρως14mo ago
yup but first, i would add a class to hidr it hide otherwise, the browser will have to re-render very frequently and can cause reflow issues
roelof
roelofOP14mo ago
oke right now the userfeedbackDiv is al the time there as a empty div
ἔρως
ἔρως14mo ago
hide it by default
roelof
roelofOP14mo ago
but we can hide it with css and make it visible when there are messages I will do that after dinner
ἔρως
ἔρως14mo ago
no, make it visible by removing a class/attribute to hide it
roelof
roelofOP14mo ago
I think we mean the same
ἔρως
ἔρως14mo ago
yes, but im being pedantic
roelof
roelofOP14mo ago
im not saying that but the code works without a problem now thinking how to make the class hidden I know how to do this with css pushed for point 1 I hope you are happy
ἔρως
ἔρως14mo ago
i will check in a bit
roelof
roelofOP14mo ago
take your time
ἔρως
ἔρως14mo ago
https://gitlab.com/roelofwobben/short-code-form/-/blob/main/js/myCustomForm.js?ref_type=heads#L53 <-- you have a bug you're saying that the constant is an array of strings however, it is a node or null const userFeedbackDiv = form.querySelector('.user_feedback'); <-- this is repeated
roelof
roelofOP14mo ago
Then it is a node
roelof
roelofOP14mo ago
I know that it exists because I put it into the html So it can never be null oke, then that part needs to be changed I have to think in what
ἔρως
ἔρως14mo ago
it's a div element
roelof
roelofOP14mo ago
oke, then I have to dive into that js thing to see how I can tell that What do you think is beter Element or HTMLElement ?
ἔρως
ἔρως14mo ago
DivHTMLElement
roelof
roelofOP14mo ago
oke and I think because of the duplication I make it a global one
ἔρως
ἔρως14mo ago
you can't what you can do is to do nothing
roelof
roelofOP14mo ago
yes, I see
ἔρως
ἔρως14mo ago
or, move the constant outside the event listener, as you should
roelof
roelofOP14mo ago
I cannot do it because I do not know the form
roelof
roelofOP14mo ago
yep I can move it outside the evenlisteners
ἔρως
ἔρως14mo ago
yup, that's where it belongs
roelof
roelofOP14mo ago
Changed it also
ἔρως
ἔρως14mo ago
https://gitlab.com/roelofwobben/short-code-form/-/blob/main/js/myCustomForm.js?ref_type=heads#L3 <-- this can move under the constant, and assign it to a constant with the same name as the function then, renove the form argument, and remove the form argument where you call the function
roelof
roelofOP14mo ago
?? That part is not in a function as far as I see it
ἔρως
ἔρως14mo ago
function showErrorMessages(messages, form) {
roelof
roelofOP14mo ago
no no, that stops right before the loop
ἔρως
ἔρως14mo ago
push it then
roelof
roelofOP14mo ago
pushed it
ἔρως
ἔρως14mo ago
the line i pointed out didn't change line 3
roelof
roelofOP14mo ago
yep, because I do not see what to change
ἔρως
ἔρως14mo ago
assign it to a constant, with the same name as the function
roelof
roelofOP14mo ago
oke line 3 looks now like this :
var showErrorMessages = function showErrorMessages(messages, form) {
var showErrorMessages = function showErrorMessages(messages, form) {
then you said , delete the form but then the first div is not found
ἔρως
ἔρως14mo ago
not yet now, grab the whole function, and move it under the constant you created with the div
roelof
roelofOP14mo ago
oke, you want the function be on line 49 ?
ἔρως
ἔρως14mo ago
yes
roelof
roelofOP14mo ago
oke, never done that , a function in a function does not know if the code is more readable but I changed it that way
ἔρως
ἔρως14mo ago
now, remove the form argument from the function declaration and where the function is called
roelof
roelofOP14mo ago
oke, did that
ἔρως
ἔρως14mo ago
can you push?
roelof
roelofOP14mo ago
yep
ἔρως
ἔρως14mo ago
fix the indentation and then add a ; on line 46
roelof
roelofOP14mo ago
done and done hmm now no front end messages anymore
ἔρως
ἔρως14mo ago
any errors?
roelof
roelofOP14mo ago
nope , not on the console I see now this
roelof
roelofOP14mo ago
No description
roelof
roelofOP14mo ago
when I submit a empty form and that seems backend validation
ἔρως
ἔρως14mo ago
let me try it do you ever pull code?
ἔρως
ἔρως14mo ago
2 hours ago, you added back a bug you fixed yesterday
roelof
roelofOP14mo ago
no
ἔρως
ἔρως14mo ago
yes you added a bug
roelof
roelofOP14mo ago
maybe I copy sometimes old code back
ἔρως
ἔρως14mo ago
probably just make sure that the css and javascript file have the same spelling
roelof
roelofOP14mo ago
I thougth I solved that bug then
ἔρως
ἔρως14mo ago
yesterday but it's back
roelof
roelofOP14mo ago
oke I changed it to :
wp_register_script( 'rw-custom-form-script', plugins_url( '/js/myCustomForm.js', __FILE__ ), array(), '1.0.0', 'all' );
wp_register_script( 'rw-custom-form-script', plugins_url( '/js/myCustomForm.js', __FILE__ ), array(), '1.0.0', 'all' );
ἔρως
ἔρως14mo ago
push it
roelof
roelofOP14mo ago
but still the same "problem"
ἔρως
ἔρως14mo ago
any errors?
roelof
roelofOP14mo ago
done no errors on the console
ἔρως
ἔρως14mo ago
what's this?
No description
roelof
roelofOP14mo ago
I do not see it
ἔρως
ἔρως14mo ago
did you refresh it without keeping the cache?
roelof
roelofOP14mo ago
and it makes no sense to me
const userFeedbackDiv = form.querySelector('.user_feedback');
userFeedbackDiv.classlist.add('hidden');
const userFeedbackDiv = form.querySelector('.user_feedback');
userFeedbackDiv.classlist.add('hidden');
I think so I see on my network tab that cache is disabled Still wonder why the userFeedbackDiv is undefined There is a div with a class user_feedback or the const should be the culprit
ἔρως
ἔρως14mo ago
it's not the classlist is undefined you misspelled it with proper jsdocs, you wouldn't misspell it you left it on line 52
roelof
roelofOP14mo ago
thanks the front end messages are back
ἔρως
ἔρως14mo ago
this is why i bug you about indentation and formatting the code and should bug you more about documentation
roelof
roelofOP14mo ago
the only thing still on the list of me is to make them look pretty and that every message has his own div
ἔρως
ἔρως14mo ago
oh, no no no you're not done
roelof
roelofOP14mo ago
oke np
ἔρως
ἔρως14mo ago
instead of line 17, you should have it hidden by default, with the class already in the template
roelof
roelofOP14mo ago
also changed
ἔρως
ἔρως14mo ago
now, under the constant. create a function to clear the contents of the error messages
roelof
roelofOP14mo ago
oke I had that function no idea where it has gone then
ἔρως
ἔρως14mo ago
doesn't matter now, you would have to move it there anyways
roelof
roelofOP14mo ago
found it back on line 52
ἔρως
ἔρως14mo ago
that's not a function it's a piece of code inside a function
roelof
roelofOP14mo ago
???
ἔρως
ἔρως14mo ago
form.addEventListener('submit', (e) => {
e.preventDefault();

/**
* @type {HTMLDivElement}
*/



while (userFeedbackDiv.firstChild) {
userFeedbackDiv.removeChild(userFeedbackDiv.firstChild);
}
form.addEventListener('submit', (e) => {
e.preventDefault();

/**
* @type {HTMLDivElement}
*/



while (userFeedbackDiv.firstChild) {
userFeedbackDiv.removeChild(userFeedbackDiv.firstChild);
}
piece of code inside a function
roelof
roelofOP14mo ago
oke so like this :
const removeChilds = function removeChilds () {
while (userFeedbackDiv.firstChild) {
userFeedbackDiv.removeChild(userFeedbackDiv.firstChild);
}
}
const removeChilds = function removeChilds () {
while (userFeedbackDiv.firstChild) {
userFeedbackDiv.removeChild(userFeedbackDiv.firstChild);
}
}
roelof
roelofOP14mo ago
I thought so
ἔρως
ἔρως14mo ago
you're close really close
roelof
roelofOP14mo ago
the while stays red
ἔρως
ἔρως14mo ago
you added it before where you declare the constant
roelof
roelofOP14mo ago
?? is userFeedBackDiv then known ? I do not think so
ἔρως
ἔρως14mo ago
show me the code
roelof
roelofOP14mo ago
var removeChilds = function removeChilds () {
while (userFeedbackDiv.firstChild) {
userFeedbackDiv.removeChild(userFeedbackDiv.firstChild);
}
}

/**
* @type {HTMLDivElement}
*/

const userFeedbackDiv = form.querySelector('.user_feedback');
var removeChilds = function removeChilds () {
while (userFeedbackDiv.firstChild) {
userFeedbackDiv.removeChild(userFeedbackDiv.firstChild);
}
}

/**
* @type {HTMLDivElement}
*/

const userFeedbackDiv = form.querySelector('.user_feedback');
ἔρως
ἔρως14mo ago
🤦‍♂️ you did exactly what i said you did 🤣
roelof
roelofOP14mo ago
yep , and I still found it wierd but maybe I oversee something you did not
ἔρως
ἔρως14mo ago
i said under the constant aka: under userFeedbackDiv
roelof
roelofOP14mo ago
yep, there was it he first time
ἔρως
ἔρως14mo ago
before, it was inside the event handler
roelof
roelofOP14mo ago
and then you said
you added it before where you declare the constant
you added it before where you declare the constant
oke
ἔρως
ἔρως14mo ago
i said this because that's what you did
roelof
roelofOP14mo ago
I have now this :
all_forms.forEach((form) => {

/**
* @type {HTMLDivElement}
*/

const userFeedbackDiv = form.querySelector('.user_feedback');

var removeChilds = function removeChilds () {
while (userFeedbackDiv.firstChild) {
userFeedbackDiv.removeChild(userFeedbackDiv.firstChild);
}
}

var showErrorMessages = function showErrorMessages(messages) {
all_forms.forEach((form) => {

/**
* @type {HTMLDivElement}
*/

const userFeedbackDiv = form.querySelector('.user_feedback');

var removeChilds = function removeChilds () {
while (userFeedbackDiv.firstChild) {
userFeedbackDiv.removeChild(userFeedbackDiv.firstChild);
}
}

var showErrorMessages = function showErrorMessages(messages) {
ἔρως
ἔρως14mo ago
that's why it's red is it still red? it shouldn't
roelof
roelofOP14mo ago
and the line stays red without a reason
ἔρως
ἔρως14mo ago
show me
roelof
roelofOP14mo ago
yep
roelof
roelofOP14mo ago
No description
ἔρως
ἔρως14mo ago
that's not red that's orange and it's orange because you're searching for that
roelof
roelofOP14mo ago
oke
ἔρως
ἔρως14mo ago
No description
roelof
roelofOP14mo ago
now the error messages stay on the screen 😦
ἔρως
ἔρως14mo ago
because you aren't done
roelof
roelofOP14mo ago
so we have to use that function somewhere
ἔρως
ἔρως14mo ago
wait, the function isn't done you have to add the hidden class before you remove the children, inside the function you created also, don't call it removeChilds because that's not what the function does i mean, it does that, but that's not it's purpose the purpose is to reset the user feedback
roelof
roelofOP14mo ago
done
ἔρως
ἔρως14mo ago
so, something like resetUserFeedback is a lot better
roelof
roelofOP14mo ago
var removeChilds = function removeChilds () {
while (userFeedbackDiv.firstChild) {
userFeedbackDiv.removeChild(userFeedbackDiv.firstChild);
}
userFeedbackDiv.classList.add('hidden');
};

var removeChilds = function removeChilds () {
while (userFeedbackDiv.firstChild) {
userFeedbackDiv.removeChild(userFeedbackDiv.firstChild);
}
userFeedbackDiv.classList.add('hidden');
};

ἔρως
ἔρως14mo ago
no no before first, you hide it, then you remove the children that order is EXTREMELLY important
roelof
roelofOP14mo ago
oke looks now like this ;

var resetUserFeedback = function resetUserFeedback () {
userFeedbackDiv.classList.add('hidden');
while (userFeedbackDiv.firstChild) {
userFeedbackDiv.removeChild(userFeedbackDiv.firstChild);
}
};


var resetUserFeedback = function resetUserFeedback () {
userFeedbackDiv.classList.add('hidden');
while (userFeedbackDiv.firstChild) {
userFeedbackDiv.removeChild(userFeedbackDiv.firstChild);
}
};

ἔρως
ἔρως14mo ago
good fix up the intentation on the entire file oh, and add jsdocs everywhere i know it's boring as fuck, but you have to do it
roelof
roelofOP14mo ago
oke, on every function ?? and constant ??
ἔρως
ἔρως14mo ago
every function, constant and variable created
roelof
roelofOP14mo ago
oke Task for tomrrow today low on energy I hope I can find the types on everything
ἔρως
ἔρως14mo ago
you will be able to
roelof
roelofOP14mo ago
I hope so
ἔρως
ἔρως14mo ago
by the way, call the function inside the form submit, as the very first thing
roelof
roelofOP14mo ago
and then we are going to look where that reset function must be used
ἔρως
ἔρως14mo ago
it's just after the e.preventDefault();
roelof
roelofOP14mo ago
oke it seems to me that we are making the code complicated but it is working again what is the advantage of making functions inside functions with variables instead of seperate ones ?
ἔρως
ἔρως14mo ago
performance and less repetition
roelof
roelofOP14mo ago
oke Nobody has learned me this before
ἔρως
ἔρως14mo ago
you don't have to re-re-re-re-re-re-re-re-re-re-re-.....-re-fetch the same element from the dom
roelof
roelofOP14mo ago
on my work they are happy with seperate functions but I heared that the persons who has to learn you something have to have very basic knowlegde about html css and js on some things I already know more then them 😦
ἔρως
ἔρως14mo ago
knowing more than them is good
roelof
roelofOP14mo ago
but also frustating because when I do a complex js challenge there is nobody who can help me to solve it im more and more on my own
ἔρως
ἔρως14mo ago
🤣
No description
roelof
roelofOP14mo ago
as a example I use theme.json for declaring colors and so on the person who is called a wordpress experts never did that He changes colors on the styles tab
ἔρως
ἔρως14mo ago
the joys of suffering from success 😂 i write scss
roelof
roelofOP14mo ago
and I think he nevers had made a template from nothing and he has to learn me wordpress 😢
ἔρως
ἔρως14mo ago
just because he doesn't know how to write php, js and css, doesn't mean he doesn't know how to use wordpress at an advanced level
roelof
roelofOP14mo ago
I wonder if I ask simple questions as why I cannot make thinsg center vertically or he said gutenberg has no slider I used google and found 3
ἔρως
ἔρως14mo ago
no slider?
roelof
roelofOP14mo ago
he said it and I use now a slider in my hero section 🙂
ἔρως
ἔρως14mo ago
what do you mean with a slider?
roelof
roelofOP14mo ago
and on most things he said there is a plugin for
roelof
roelofOP14mo ago
eedee
Plugin Directory
WordPress Slider Block Gutenslider
Slider plugin for Gutenberg. Create a image slider, video slider, fullscreen slider with this powerful slider block and add any block on top.
roelof
roelofOP14mo ago
sorry for the rant but I had to get this of my chest
ἔρως
ἔρως14mo ago
oh, a slideshow yes, it doesn't have one by default but you can add it with plugins and stuff
roelof
roelofOP14mo ago
I used that one
ἔρως
ἔρως14mo ago
is it any good?
roelof
roelofOP14mo ago
first challenge for template learning is to convrt this one to a wordpress block theme https://www.free-css.com/free-css-templates/page293/bricker
Bricker Free Website Template | Free CSS Templates | Free CSS
Preview of the Bricker Free CSS Template from Vision Design
roelof
roelofOP14mo ago
I got a working with some text
ἔρως
ἔρως14mo ago
nice
roelof
roelofOP14mo ago
im now half way the first page If you want I can put my template also on gitlab But it will be Monday Next Friday I got a taste of Blender
ἔρως
ἔρως14mo ago
blender sounds really fun, but it isn't for me
roelof
roelofOP14mo ago
for me no idea Never tried it I noticed when I do developing work , I end up very much in my head instead of in my body and that is not healty for mental health
ἔρως
ἔρως14mo ago
no, it isn't but you also overcomplicate and freak out super easily
roelof
roelofOP14mo ago
I know I think I set the bar very high for me
ἔρως
ἔρως14mo ago
you do, but then you don't do important (but simple) things like indentation you have the bar super high in some places, but not high enough in others
roelof
roelofOP14mo ago
I know still learning to be nicer to myself and learning how to get more peace in my mind and body a process of trail and error
ἔρως
ἔρως14mo ago
peace in your mind is the most important
roelof
roelofOP14mo ago
I know plan for tomorrow do the jsdocs and think how I can make the backend in the right format looks to me they were but we convert it to json
ἔρως
ἔρως14mo ago
something else you should do: lock the button to avoid submitting 10000000 times and also give feedback to the user that the form is sending
roelof
roelofOP14mo ago
so I have to find a way to convert it back then yep, therefore I think I need to iterate through the backend response
ἔρως
ἔρως14mo ago
you have to grab what the server returns and make it compatible with your function
roelof
roelofOP14mo ago
and then look if I need to use the error class or success class yep, that is a array of strings that is what the feedback function needs
ἔρως
ἔρως14mo ago
it is but it isn't all it's an array within and object within an object
roelof
roelofOP14mo ago
you mean the backend response ? and every object has a code for success or fail and a message Like I said I have to look well into this first the jsdoc then the converting
ἔρως
ἔρως14mo ago
yes, jsdoc you should do phpdoc as well
roelof
roelofOP14mo ago
one problem at the time
ἔρως
ἔρως14mo ago
it will help you yes
roelof
roelofOP14mo ago
pity I cannot see the contents in a console like js can
ἔρως
ἔρως14mo ago
but since javascript is what's giving you the most problem, i asked it first you can
roelof
roelofOP14mo ago
would sometimes be easy
ἔρως
ἔρως14mo ago
it isn't easy on windows, but you can
roelof
roelofOP14mo ago
oke surprised but for now GN
ἔρως
ἔρως14mo ago
goodnight
roelof
roelofOP14mo ago
I use always var_dump but that push it into the browser
ἔρως
ἔρως14mo ago
yes, but you can write to an error log too but that's for tomorrow go rest
roelof
roelofOP14mo ago
yes boss
ἔρως
ἔρως14mo ago
🫡
roelof
roelofOP14mo ago
I messed up the code somehow. I added as much jsodc and phpdoc as I could But when I now open the site I get a nonce error As far as I know that one should be when submitting the form not with showing the form latest code is pushed problem solved oke we get this back

Object { success: true }

success: true

Object { success: true }

success: true
from the backend I have to think well how I can make it work because showErrorMessages takes in a array of strings and here no string and this is a error message from the backend :
data: Array [ {…} ]
​​
0: Object { code: "Error", message: "Form is messed up" }
​​
length: 1
​​
<prototype>: Array []
data: Array [ {…} ]
​​
0: Object { code: "Error", message: "Form is messed up" }
​​
length: 1
​​
<prototype>: Array []
This one is easier
because there is a message but as I said the display takes only the message I have to think how I can use the status We have three different inputs 1) from the front end a array of strings 2) Success from the backend (a object) 3) Fail from the backend( a array of strings or a object( depending on 1 or more errors)
ἔρως
ἔρως14mo ago
you need a different function to render the success
roelof
roelofOP14mo ago
could also be a solution but I was thinkig about 1 function because the showing the errors and showing the success has a lot of duplicate except the class
ἔρως
ἔρως14mo ago
then you will have to take an extra argument in the existing function, and rename it to something better
roelof
roelofOP14mo ago
That is what I already thought and then with a if then make a error or a success class but first I have to figure out oif I get a object or a array of object Then I could send the status and the messages
ἔρως
ἔρως14mo ago
that works too
roelof
roelofOP14mo ago
so a few small steps and the code is done then find good css for the messages Clean up the css and Ship it
ἔρως
ἔρως14mo ago
basically, yup
roelof
roelofOP14mo ago
finally so 2 - 3 days work for me 🙂 hmm first problem How can I re-use the reset and show errors when they under it declared in the loop for a submit event ? latest code is pushed
ἔρως
ἔρως14mo ago
not really it will be faster well, you can use the same way it doesnt change a lot
roelof
roelofOP14mo ago
Can I I tried this :
async function send_to_backend(data, url) {
var response = await fetch(url, {
method: 'POST',
body: data
})
var data = await response.json();
if (typeof(data) == 'object') {
resetUserFeedback();
showErrorMessages("Mail has been send", "success")
}
}
async function send_to_backend(data, url) {
var response = await fetch(url, {
method: 'POST',
body: data
})
var data = await response.json();
if (typeof(data) == 'object') {
resetUserFeedback();
showErrorMessages("Mail has been send", "success")
}
}
but I see then a message that resetUserFeedback is not known
ἔρως
ἔρως14mo ago
you're doing it wrong the function to send a request to the backend should do NOTHING ELSE here, you are trying to break that
roelof
roelofOP14mo ago
oke, changed that code to the end
/**
* Send the form data to the backend
*/
var backend_response = send_to_backend(formdata, form.action);

if (typeof(backend_response) == 'object') {
resetUserFeedback();
showErrorMessages("Mail has been send", "success")
}
})
/**
* Send the form data to the backend
*/
var backend_response = send_to_backend(formdata, form.action);

if (typeof(backend_response) == 'object') {
resetUserFeedback();
showErrorMessages("Mail has been send", "success")
}
})
and I get this one :
Uncaught TypeError: messages.forEach is not a function
Uncaught TypeError: messages.forEach is not a function
so it looks like the fist argument needs to be a array solved
ἔρως
ἔρως14mo ago
yup, it needs to be an array is it working?
roelof
roelofOP14mo ago
almost the problem I have now is this code :
const errorDivElement = form.querySelector('.error');




/**
* Loop through the error messages array and create HTML elements for each message
*/
messages.forEach(message => {
// Create a new <p> element
const errorParagraph = document.createElement('p');

// Set the text content of the <p> element to the current error message
errorParagraph.textContent = message;

// Append the <p> element to the error div
errorDivElement.appendChild(errorParagraph);
});
};
const errorDivElement = form.querySelector('.error');




/**
* Loop through the error messages array and create HTML elements for each message
*/
messages.forEach(message => {
// Create a new <p> element
const errorParagraph = document.createElement('p');

// Set the text content of the <p> element to the current error message
errorParagraph.textContent = message;

// Append the <p> element to the error div
errorDivElement.appendChild(errorParagraph);
});
};
when there is only a sucess errorDivElement is not nilll so I think I check for that and if so I do a querySelector for the success class but first a late lunch and when I solved that one I have to see how to do this for a error Thinking of iterate throught it and get all messages out of the objects and then I can do the same as a success that part is working
roelof
roelofOP14mo ago
No description
roelof
roelofOP14mo ago
I know its ugly colors but I first want to make things work Everything seem to work Pushed the latest code if you do not find any problems then clean up the css ,make the messages nice and find a way that a user cannot submit multiple times and then we finally finished
ἔρως
ἔρως14mo ago
i have a simple way: disable the submit button but you know what's even better? a spinner on the button
roelof
roelofOP14mo ago
looks good a spinner I have to google again how to achieve that
roelof
roelofOP14mo ago
W3Schools online HTML editor
The W3Schools online code editor allows you to edit code and view the result in your browser
ἔρως
ἔρως14mo ago
you can do something a lot simpler on the ::before, set a width and height of about 1.5em then, set the border to about 0.25em white and the top, set to something like dark blur blue on an infinite linear animation, rotate it 360 degrees
roelof
roelofOP14mo ago
that I can do but do we not need some code to or display submit or display the spinner ? and the before you mean the before of the button ?
ἔρως
ἔρως14mo ago
i didnt say "before", i said "::before" its a very importance difference you can display the spinner when the button is disabled and that is as simple as setting the button to disabled before clearing the messages, and after you receive an answer
roelof
roelofOP14mo ago
looks complicated thinkng now to go for the simple solution
ἔρως
ἔρως14mo ago
its only complicated in your head you simply grab the submit button then, inside the submit handler, simply set the disabled attribute to itself, or set the property to true in the css, simply add the styles to add the spinner when the button is disabled
roelof
roelofOP14mo ago
oke
ἔρως
ἔρως14mo ago
you can do it
roelof
roelofOP14mo ago
I know I can I onky have to find some mental and fysical energy to do it Yesterday and the day before yesterday I had some energy. Today totally no energy Stupid body and mind
ἔρως
ἔρως14mo ago
means you need a break
roelof
roelofOP14mo ago
Think I need a lot of breaks today but that is what it is
ἔρως
ἔρως14mo ago
just take the break
roelof
roelofOP14mo ago
im busy with it tomorrow the spinner and I see when the css is on my list im already very very happy that the plugin works and yes, I know there are some issues but they can be easily solved so many manu thanks for your patience with me I think im not the easiest student
ἔρως
ἔρως14mo ago
there are much much worse students i know that if i explain something to you, usually 3rd time's the charm
roelof
roelofOP14mo ago
oke, and Im sure that there are also a lot of better students
ἔρως
ἔρως14mo ago
even if the code comes out looking like it went through a woodchipper...
roelof
roelofOP14mo ago
🙂
ἔρως
ἔρως14mo ago
you still do something and move forward on your own and that is important
roelof
roelofOP14mo ago
yep, im following my way and that is the only good way
ἔρως
ἔρως14mo ago
you still have the difficulties ive pointed out before, but you get there with time
roelof
roelofOP14mo ago
we see
ἔρως
ἔρως14mo ago
there are ways better than yours and mine, and sometimes, its good to use those ways
roelof
roelofOP14mo ago
I learned that I make the best progress small steps at the time I mean my way in life
ἔρως
ἔρως14mo ago
same here
roelof
roelofOP14mo ago
work to do :" - make the spinner - clean up the css _ make the messages nice - make the form empty after submit and sending the email
ἔρως
ἔρως14mo ago
all easy stuff the last one is just form.reset()
roelof
roelofOP14mo ago
oke, then I was overthinking that one also and for the 3th one I will google for some inspiration
ἔρως
ἔρως14mo ago
well, just use the colors from taileibd tailwind and reduce the HUGE border radius to 5 or 7px
roelof
roelofOP14mo ago
oke
ἔρως
ἔρως14mo ago
its easier than you think
roelof
roelofOP14mo ago
we see like i said one problem at the time
ἔρως
ἔρως14mo ago
exactly, one at a time
roelof
roelofOP14mo ago
and for the toasters
roelof
roelofOP14mo ago
The soft ones of this page : https://preline.co/docs/toasts.html
Preline
Tailwind CSS Toasts | Preline UI, crafted with Tailwind CSS
Push notifications to your visitors with a toast, a lightweight and easily customizable alert message.
ἔρως
ἔρως14mo ago
yup or any that you like
roelof
roelofOP14mo ago
I liked these But no idea if its looking well with the form one way of finding out is trying out
ἔρως
ἔρως14mo ago
THERE! that's how you find out! see? now you're getting it
roelof
roelofOP14mo ago
🎉
ἔρως
ἔρως14mo ago
you're evolving
roelof
roelofOP14mo ago
Yep and that is good in life
ἔρως
ἔρως14mo ago
it is very good it also means you won't need as much help too, and can find stuff yourself
roelof
roelofOP14mo ago
Hope so Curious what the New one Will be and if and how i can imprive ny react component But as i aaid earlier one problemen at the time
ἔρως
ἔρως14mo ago
yes, you need to finish this one first
roelof
roelofOP14mo ago
Exactly And in small steps
ἔρως
ἔρως14mo ago
yes, small steps is important
roelof
roelofOP14mo ago
Learned that the hard way So you think.im could be a goed junior at my old age?
ἔρως
ἔρως14mo ago
yes, you just need to practice more to be a solid developer
roelof
roelofOP14mo ago
That is why i found the site that sinds you a challenge once a month
ἔρως
ἔρως14mo ago
it is a good start
roelof
roelofOP14mo ago
To learn wordpress and practice Maybe later i xan make small projects for clients
ἔρως
ἔρως14mo ago
you still need a bit more of practice, but yes
roelof
roelofOP14mo ago
That is what i mean
ἔρως
ἔρως14mo ago
oh well, you're pretty close
ἔρως
ἔρως14mo ago
https://jsfiddle.net/kguhyte8/ <-- by the way, i made you an example of how you can implement the spinning
Edit fiddle - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
roelof
roelofOP14mo ago
Thanks i Will study it looks easy
ἔρως
ἔρως14mo ago
it's easier than you thought, right?
roelof
roelofOP14mo ago
yep but that will be , just as the toaster colors, tomorrow maybe if I have the energy I can do all 4 last points otherwise I will be Friday afternoon
ἔρως
ἔρως14mo ago
you should rest today, we can do it tomorrow
roelof
roelofOP14mo ago
exactly im wathing tv but I hate commercials
ἔρως
ἔρως14mo ago
as we say here: tomorrow is still a day so, you have time
roelof
roelofOP14mo ago
I have , the person who made the challenges say make them in your own time in your own pace for me it is alll about learning and having fun
roelof
roelofOP14mo ago
What do you think about this :
No description
roelof
roelofOP14mo ago
one "problem"
if I do this :
var backend_response = send_to_backend(formdata, form.action);

if (typeof(backend_response) == 'object') {
resetUserFeedback();
showErrorMessages(["Mail has been send"], "success");
form.reset();
} else {
resetUserFeedback();
//take all the messages out of the array of object
let messages = backend_response.map(item => item.messages);
// show the messages
showErrorMessages(messages , "Error");
}
})
var backend_response = send_to_backend(formdata, form.action);

if (typeof(backend_response) == 'object') {
resetUserFeedback();
showErrorMessages(["Mail has been send"], "success");
form.reset();
} else {
resetUserFeedback();
//take all the messages out of the array of object
let messages = backend_response.map(item => item.messages);
// show the messages
showErrorMessages(messages , "Error");
}
})
then the form stays at the input that is given by the shortcode Is that right ? and is that what we want ?
ἔρως
ἔρως14mo ago
that code will never do what you want because an error is still an object you have to see what the server returns on an error and on success
roelof
roelofOP14mo ago
I know that already if there is a error the backend returns a array of object and when it is a success it returns a object
ἔρως
ἔρως14mo ago
this still wont work as an array is an object
roelof
roelofOP14mo ago
oke now im confused because yesterday you told me that empty a form is just easy as form.reset() And now you are saying it will not work
ἔρως
ἔρως14mo ago
im talking about the if
roelof
roelofOP14mo ago
wierd. that I then see the right color on a error or a success
ἔρως
ἔρως14mo ago
thats weird indeed, but dont change if it works
roelof
roelofOP14mo ago
you are right I forget to test the case that if the nonce is not right and I got a message mail is send so back to the drawing table how to check if it's a object or a array of objects 😦
ἔρως
ἔρως14mo ago
you are overthinking just check what the success and error responses have
roelof
roelofOP14mo ago
?? or do you mean somethhing like this :
if(backend_response.success) {)
if(backend_response.success) {)
nope, it does not work pity success returns this :
Object { success: true }
Object { success: true }
I see now this error message in the console :
Uncaught TypeError: backend_response.map is not a function
Uncaught TypeError: backend_response.map is not a function
hmm, it schould return a array there :
data: Array [ {…} ]
​​
0: Object { code: "Error", message: "Form is messed up" }
​​
length: 1
​​
<prototype>: Array []
data: Array [ {…} ]
​​
0: Object { code: "Error", message: "Form is messed up" }
​​
length: 1
​​
<prototype>: Array []
chips, it is still a Promise
Promise { <state>: "fulfilled", <value>: undefined }

<state>: "fulfilled"

<value>: undefined

<prototype>: Promise.p
Promise { <state>: "fulfilled", <value>: undefined }

<state>: "fulfilled"

<value>: undefined

<prototype>: Promise.p
ἔρως
ἔρως14mo ago
this is supposed to work but it doesnt work because you are doing something wrong
roelof
roelofOP14mo ago
Yep i know. Somewhere it changed from a object to 's promise the problem is here :
/**
* Function to send form data to the backend
* @param {FormData} data - The form data
* @param {string} url - The backend URL
* @returns {Promise} The response from the backend
*/
async function send_to_backend(data, url) {
var response = await fetch(url, {
method: 'POST',
body: data
})
var data = await response.json();
}
/**
* Function to send form data to the backend
* @param {FormData} data - The form data
* @param {string} url - The backend URL
* @returns {Promise} The response from the backend
*/
async function send_to_backend(data, url) {
var response = await fetch(url, {
method: 'POST',
body: data
})
var data = await response.json();
}
this gives a promise and not the expected output so this lime :
let messages = backend_response.map(item => item.messages);

let messages = backend_response.map(item => item.messages);

will not work because it expect a array The only thing I changed is delete the console.log(data) back to the manual how to solve this Any hints ?
ἔρως
ἔρως14mo ago
you are awaiting the json remove the await
roelof
roelofOP14mo ago
not good backend-response is still a promise
Promise { <state>: "fulfilled", <value>: undefined }
Promise { <state>: "fulfilled", <value>: undefined }
maybe use a .then somehow see also here :
roelof
roelofOP14mo ago
No description
roelof
roelofOP14mo ago
wierd , when I do a console.log in the async one I see this :
No description
roelof
roelofOP14mo ago
very wierd it looks like the console.log is getting the value but backend-response do not async is very confusing I can do this :
var backend_response = [];

/**
* Send the form data to the backend
*/
send_to_backend(formdata, form.action).then ((data) => backend_response = data);
var backend_response = [];

/**
* Send the form data to the backend
*/
send_to_backend(formdata, form.action).then ((data) => backend_response = data);
but then backend_response keeps to be a empty array
ἔρως
ἔρως14mo ago
that's not how it works did you push?
roelof
roelofOP14mo ago
moment I pushed now
ἔρως
ἔρως14mo ago
alright, i will check in a bit
roelof
roelofOP14mo ago
as always take your time
roelof
roelofOP14mo ago
done and done
ἔρως
ἔρως14mo ago
that should do it
roelof
roelofOP14mo ago
oke nope. still this message :
Uncaught (in promise) TypeError: backend_response.map is not a function
Uncaught (in promise) TypeError: backend_response.map is not a function
ἔρως
ἔρως14mo ago
that's a bug you added the response may or may not be an array
roelof
roelofOP14mo ago
When it is a error it is as far as I know always a array
ἔρως
ἔρως14mo ago
if it is an array, you know it's an error, so, just assume it's errors and pass imediatelly to the function showErrorMessages <-- this one
roelof
roelofOP14mo ago
when it is a error on the backend It is in this format
data: Array [ {…} ]
​​
0: Object { code: "Error", message: "Form is messed up" }
​​
length: 1
​​
<prototype>: Array []
data: Array [ {…} ]
​​
0: Object { code: "Error", message: "Form is messed up" }
​​
length: 1
​​
<prototype>: Array []
as far as I can see backend_response is still a promise here
Promise { <state>: "pending" }

<state>: "fulfilled"

<value>: undefined

<prototype>: Promise.prototype { … }
Promise { <state>: "pending" }

<state>: "fulfilled"

<value>: undefined

<prototype>: Promise.prototype { … }
ἔρως
ἔρως14mo ago
i will test in a bit after i finish eating
roelof
roelofOP14mo ago
take your time and enjoy your meal I will bring my daugther to bed
ἔρως
ἔρως14mo ago
alright, take your time too
roelof
roelofOP14mo ago
I will
roelof
roelofOP14mo ago
First three are already changed it looks like the last version is not pushed sorry
ἔρως
ἔρως14mo ago
that's why you gotta make sure you always push
roelof
roelofOP14mo ago
last point I cannot send it to errorMessages yet because it is not in the right form That is why I try to take the messages out of all the objects and then send to errorMessages
ἔρως
ἔρως14mo ago
then check if it is an array if it isn't, convert to an array by the way, i can't submit the form
ἔρως
ἔρως14mo ago
the nonce field isn't there
No description
roelof
roelofOP14mo ago
and why schould I check if its a array , Im sure it is yep, I deleted the nonce check to see if the errrors on the backend were working and now it looks like not and the most wierd is that it has worked at a point and now not and im not seeing why not
ἔρως
ἔρως14mo ago
did you fix what i told you?
roelof
roelofOP14mo ago
I did not make the change for checking for a error
ἔρως
ἔρως14mo ago
that's fine, and everything else?
roelof
roelofOP14mo ago
the rest I changed and it did not fixed the problem here
roelof
roelofOP14mo ago
im still seeing this ;
No description
ἔρως
ἔρως14mo ago
show me the response from the server
roelof
roelofOP14mo ago
you mean that I still get a promise or the error object ?
ἔρως
ἔρως14mo ago
the response from the server
roelof
roelofOP14mo ago
I can only find this :
Promise { <state>: "pending" }

<state>: "fulfilled"

<value>: undefined

<prototype>: Promise.prototype { … }
Promise { <state>: "pending" }

<state>: "fulfilled"

<value>: undefined

<prototype>: Promise.prototype { … }
ἔρως
ἔρως14mo ago
that's not the answer from the server response
roelof
roelofOP14mo ago
I hope you mean this :
No description
ἔρως
ἔρως14mo ago
close, but no that's the console, showing me a promise
roelof
roelofOP14mo ago
you mean this :
No description
ἔρως
ἔρως14mo ago
still the wrong tab
roelof
roelofOP14mo ago
this is when I console.log data oke
ἔρως
ἔρως14mo ago
this
No description
roelof
roelofOP14mo ago
you mean this :
roelof
roelofOP14mo ago
No description
roelof
roelofOP14mo ago
I do not see that in my tabs
ἔρως
ἔρως14mo ago
yes, but when you submit the form
roelof
roelofOP14mo ago
I did that did not change anything still see the same on the network tab
ἔρως
ἔρως14mo ago
that's because you selected to only show html while you're doing a fetch which is also known as ajax but used to be called xhr
roelof
roelofOP14mo ago
oke
roelof
roelofOP14mo ago
then I see this :
No description
roelof
roelofOP14mo ago
which is the expected response and as I said several times It is a array
ἔρως
ἔρως14mo ago
no, it isn't it's an object or an associative array in php, but an object in js and json
roelof
roelofOP14mo ago
he . [ ] looks a array to me
ἔρως
ἔρως14mo ago
yes, but it's inside an object it's the data property
roelof
roelofOP14mo ago
oke so we have to send the object and then the data object to the function back ?
ἔρως
ἔρως14mo ago
none you have to access the data property that's all
roelof
roelofOP14mo ago
oke then I have to think how i change this line then
var backend_response = send_to_backend(formdata, form.action)
var backend_response = send_to_backend(formdata, form.action)
ἔρως
ἔρως14mo ago
you don't that's just it oh, you need the await
roelof
roelofOP14mo ago
moment
ἔρως
ἔρως14mo ago
if it doesn't work, remove the await
roelof
roelofOP14mo ago
you just said we need to access the data property
ἔρως
ἔρως14mo ago
yes but it isn't in that line
roelof
roelofOP14mo ago
and as far as I know we do not do it at the moment so I try to find out how and where we schould do that
ἔρως
ἔρως14mo ago
where are you checking the errors?
roelof
roelofOP14mo ago
here :
var backend_response = send_to_backend(formdata, form.action);

if (backend_response['success']) {
showErrorMessages(["Mail has been send"], "success");
form.reset();
} else {
// take all the messages out of the array of object
let messages = backend_response.map(item => item.messages);
// show the messages
showErrorMessages(messages , "Error");
}
})
var backend_response = send_to_backend(formdata, form.action);

if (backend_response['success']) {
showErrorMessages(["Mail has been send"], "success");
form.reset();
} else {
// take all the messages out of the array of object
let messages = backend_response.map(item => item.messages);
// show the messages
showErrorMessages(messages , "Error");
}
})
ἔρως
ἔρως14mo ago
can you narrow it down?
roelof
roelofOP14mo ago
so we could do in on the messages line there we need the messages
ἔρως
ἔρως14mo ago
show me what you have in mind
roelof
roelofOP14mo ago
I was thinking of this:
let messages = backend_response['data'].map(item => item.messages);
let messages = backend_response['data'].map(item => item.messages);
ἔρως
ἔρως14mo ago
do it but why the ['data'] and not .data?
roelof
roelofOP14mo ago
I did it and see this :
Uncaught (in promise) TypeError: backend_response.data is undefined
Uncaught (in promise) TypeError: backend_response.data is undefined
ἔρως
ἔρως14mo ago
is it a promise?
roelof
roelofOP14mo ago
if I look at the networktab not but the compiler says it is
ἔρως
ἔρως14mo ago
then remove the await i told you to add
roelof
roelofOP14mo ago
oke O , that one was without the await
ἔρως
ἔρως14mo ago
then add it
roelof
roelofOP14mo ago
with the await I see this :
Uncaught (in promise) TypeError: backend_response is undefined
Uncaught (in promise) TypeError: backend_response is undefined
ἔρως
ἔρως14mo ago
can you push it?
roelof
roelofOP14mo ago
yep and I take a shower
roelof
roelofOP14mo ago
oke I see now a error field without any text so we getting closer
ἔρως
ἔρως14mo ago
did you push?
roelof
roelofOP14mo ago
nope
ἔρως
ἔρως14mo ago
push it
roelof
roelofOP14mo ago
did it oke, the messages line is not good messages is empty
roelof
roelofOP14mo ago
yes , finally working again
roelof
roelofOP14mo ago
No description
roelof
roelofOP14mo ago
and now really hit the shower tomorrow I will try if the success is working and the front end validation and then add the spinner
ἔρως
ἔρως14mo ago
you do have a fair bit to cleanup in your showErrorMessages looks like a dare devil went buckwild there and stuff's not pretty 🤣 but go shower, rest, watch something, sleep and we finish this tomorrow
roelof
roelofOP14mo ago
oke boss
ἔρως
ἔρως14mo ago
🤣
roelof
roelofOP14mo ago
oke, could not sleep So I tested and all messages appear as I wanted tomorrow the spinner 🙂 GN
ἔρως
ἔρως14mo ago
goodnight
roelof
roelofOP14mo ago
But how so a bug cleanup. We made the code ourselves
ἔρως
ἔρως14mo ago
there's still a fair bit to be done
roelof
roelofOP14mo ago
oke im ready when you are ready and I still have to implement that spinner 🙂
ἔρως
ἔρως14mo ago
you can start there
roelof
roelofOP14mo ago
oke, So set the disabled on submitting and set the disabed to false when the server sends back the response Did implement it as I thought you told me but the button has all the time a disabled color will push the code
ἔρως
ἔρως14mo ago
i will check later
roelof
roelofOP14mo ago
as I said many times take all the time you need
ἔρως
ἔρως14mo ago
well, this time, i have no choice because im working
roelof
roelofOP14mo ago
work is also important
ἔρως
ἔρως14mo ago
yes, but tiring specially when i woke up 3 hours late
roelof
roelofOP14mo ago
ooo, that is not good here time for dinner/supper We can also work tomorrow or Sunday if you want some rest
ἔρως
ἔρως14mo ago
i still need to work 2 hours
roelof
roelofOP14mo ago
oops then you are working very very late
ἔρως
ἔρως14mo ago
just a little
roelof
roelofOP14mo ago
a little. 2 hours work from 19: 22 is 21:22 and I find that very very late
ἔρως
ἔρως14mo ago
it is late, but im 1 hour behind you
roelof
roelofOP14mo ago
oke then it is still late I think in the Netherlands nobody is working after 18:00 hours
ἔρως
ἔρως14mo ago
yeah, i hate working past 6pm
roelof
roelofOP14mo ago
Success Just watching tv with my family
ἔρως
ἔρως14mo ago
enjoy the tv im procrastinating
roelof
roelofOP14mo ago
Your online human Only human
ἔρως
ἔρως14mo ago
is that a series?
roelof
roelofOP14mo ago
No I try to tell you that procrasting is normal because you are a human I Will see when you are going to help me with the last pieces
ἔρως
ἔρως14mo ago
well, i procrastinated an hour and a half, so far 🤣
roelof
roelofOP14mo ago
Nothing wrong with that Take some rest and we see when we can end thuis No hurry
ἔρως
ἔρως14mo ago
im just hurried for work 😦
roelof
roelofOP14mo ago
sleep a lot my friend
ἔρως
ἔρως14mo ago
i will need
roelof
roelofOP14mo ago
exactly your healt is much and much more important then everything for me it is almost bed time
ἔρως
ἔρως14mo ago
for me, its screaming time
roelof
roelofOP14mo ago
screaming time ?? how so ??
ἔρως
ἔρως14mo ago
so, basically, i had to replicate wordpress' url handling in laravel so i can get the same url for the posts, from laravel and wordpress
roelof
roelofOP14mo ago
pfff, who has thought that was a good idea I will tomorrow look for myself why the disableling is not working I do not see the messages anymore oke, line 86 is too blame
ἔρως
ἔρως14mo ago
🤣 it's good that you're finding your blunders before i look into it button.addAttribute('disabled'); <-- you're right, this is the line to blame 😂
roelof
roelofOP14mo ago
yep, if I delete it , everything is working
ἔρως
ἔρως14mo ago
yup, it's because there's no addAttribute it's setAttribute
roelof
roelofOP14mo ago
so I use the wrong function or the right function on the wrong place
ἔρως
ἔρως14mo ago
none you tried to use a function that doesn't exist
roelof
roelofOP14mo ago
oke, a little buit further I see the errror messages but the button stays disabled it works now only find why I do not see the spinner but here time to sleep and I think also for you
roelof
roelofOP14mo ago
oke for me its is 22:10 so almost time to sleep I can delete it but then the code runs the backend code even if there are errors
ἔρως
ἔρως14mo ago
that's not the problem the problem is that you aren't removing the attribute there
roelof
roelofOP14mo ago
oke, but that is the part above it you pointed to the return
ἔρως
ἔρως14mo ago
🤔 this is a mistery for tomorrow then
roelof
roelofOP14mo ago
I will sleep about it what is wrong with this line
button.removeAttribute('disabled');
button.removeAttribute('disabled');
ἔρως
ἔρως14mo ago
https://gitlab.com/roelofwobben/short-code-form/-/blob/main/js/myCustomForm.js?ref_type=heads#L85 <-- this is wrong it's e.submitter or just find all submit buttons inside the form this is better because the button may be outside the form
roelof
roelofOP14mo ago
oke, I try to find the button and now there is only 1 at a form
ἔρως
ἔρως14mo ago
just rest, we finish tomorrow
roelof
roelofOP14mo ago
oke GN
ἔρως
ἔρως14mo ago
goodnight
roelof
roelofOP14mo ago
just rolled out of bed and ready to solve the last bugs
ἔρως
ἔρως14mo ago
did you try the changes i told you?
roelof
roelofOP14mo ago
yep and still no spinner
ἔρως
ἔρως14mo ago
did you push?
roelof
roelofOP14mo ago
yes did I jisy again to be certain
ἔρως
ἔρως14mo ago
you still didn't fix this
No description
ἔρως
ἔρως14mo ago
No description
ἔρως
ἔρως14mo ago
No description
ἔρως
ἔρως14mo ago
No description
roelof
roelofOP14mo ago
Can be, I never tested it against a empty input But I expect that the front end validation would take that if not, I have to think well how to do it then
ἔρως
ἔρως14mo ago
you have to extract all errors
roelof
roelofOP14mo ago
I thought I did
roelof
roelofOP14mo ago
when I do a empty shortcode I see this :
No description
roelof
roelofOP14mo ago
surprising Did not expect to see that
ἔρως
ἔρως14mo ago
yeah, because ... the $atts variable is an empty string when there's no attributes 🤦‍♂️ just don't try to understand that cast it to an array and it fixes this
roelof
roelofOP14mo ago
I thought I did that You told me a few days ago to do so only in the form.php or also in the email.php ? yep solved that case
ἔρως
ἔρως14mo ago
yup, 1 bug down 1 more to go
roelof
roelofOP14mo ago
oke hopefully it is not a difficult one
ἔρως
ἔρως14mo ago
it's not do this: try to get as many server-side errors as you can and then, compare the structure of the errors that you receive
roelof
roelofOP14mo ago
hmm, then I have to think well how so most validation errrors are already found by the front end validation
ἔρως
ἔρως14mo ago
the exact same way i did to show you what the server returns comment it out for a short while, while you debug the server side validation
roelof
roelofOP14mo ago
oke, that is simple
ἔρως
ἔρως14mo ago
you can also edit and re-run the request in firefox you can even try to use something like postman
roelof
roelofOP14mo ago
oke
roelof
roelofOP14mo ago
then I see this :
No description
roelof
roelofOP14mo ago
which looks well and it also shown well on the form
ἔρως
ἔρως14mo ago
did you push?
roelof
roelofOP14mo ago
here a beter view :
No description
ἔρως
ἔρως14mo ago
are you sure we're running the same code?
roelof
roelofOP14mo ago
just pushed I deleted all the if then 's in the myCustomForm.js
ἔρως
ἔρως14mo ago
i see what the problem is
roelof
roelofOP14mo ago
and checked they are the backend validations
ἔρως
ἔρως14mo ago
compare line 12, 28 and 59
roelof
roelofOP14mo ago
of myCustumForm.js ?
ἔρως
ἔρως14mo ago
submit.php
roelof
roelofOP14mo ago
line 12 : $error = new WP_Error(400, "Bad request") ; line 28: $errors = new WP_Error(); line 59 : wp_send_json(new WP_Error("Error", "Mail cannot be send"));
ἔρως
ἔρως14mo ago
do you see the difference?
roelof
roelofOP14mo ago
yep, but that are all different cases on line 12 there is a fixed error On line 28 I made the object so I can add the error messages later and on line 59 I send a wp-error with the statuscode of Error with a fixed message
ἔρως
ἔρως14mo ago
here's what you can do create the same error at the top, what you're doing for line 28 and then, just add the error to it, and where you have to stop you send the error https://gitlab.com/roelofwobben/short-code-form/-/blob/main/submit.php?ref_type=heads#L56 <-- by the way, the array() format is super old school - from php 5.3 and older
roelof
roelofOP14mo ago
so something of this :
$path = preg_replace('/wp-content.*$/', '', __DIR__);
require_once($path . "wp-load.php");

$errors = new WP_Error();

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
$error = $errors->add (400, "Bad request") ;
wp_send_json_error($error);
}
$path = preg_replace('/wp-content.*$/', '', __DIR__);
require_once($path . "wp-load.php");

$errors = new WP_Error();

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
$error = $errors->add (400, "Bad request") ;
wp_send_json_error($error);
}
ἔρως
ἔρως14mo ago
yup
roelof
roelofOP14mo ago
oke, so I have to make 2 lines of this one :

wp_send_json(new WP_Error("Error", "Mail cannot be send"));

wp_send_json(new WP_Error("Error", "Mail cannot be send"));
ἔρως
ἔρως14mo ago
yup
roelof
roelofOP14mo ago
and for this one I think I cannot make 2 lines
wp_send_json(['success' => true]);
wp_send_json(['success' => true]);
ἔρως
ἔρως14mo ago
it's fine
roelof
roelofOP14mo ago
oke
ἔρως
ἔρως14mo ago
that one isn't an error
roelof
roelofOP14mo ago
oke, pushed it
ἔρως
ἔρως14mo ago
test it make sure it works on your end
roelof
roelofOP14mo ago
for me it still works , even before this change
roelof
roelofOP14mo ago
No description
roelof
roelofOP14mo ago
I changed the text so I could see the difference between front and back validation
ἔρως
ἔρως14mo ago
for me, it failed on sending the email, so, try there force it to fail
roelof
roelofOP14mo ago
wierd, for me it even comes to sending a email could it be a browser thing I use FF all the time I forced it to fail by sending a empty form
ἔρως
ἔρως14mo ago
and it works?
roelof
roelofOP14mo ago
yep I send you the screenshots a few minutes ago and I see the same before we changed the error code so I cannot reproduce your problem sorry
ἔρως
ἔρως14mo ago
are you sure you pushed?
ἔρως
ἔρως14mo ago
i still see this in gitlab
No description
roelof
roelofOP14mo ago
wierd, IM fairly sure I commited but I will try again
ἔρως
ἔρως14mo ago
try it
roelof
roelofOP14mo ago
still see this when I send a empty form
No description
roelof
roelofOP14mo ago
still not the error you see
ἔρως
ἔρως14mo ago
this is the last error when the email fails to send
roelof
roelofOP14mo ago
o, wait then I have to fill in the form moment nope, still working here
roelof
roelofOP14mo ago
No description
roelof
roelofOP14mo ago
no errors on both cases on a empty form and on a filled in form
roelof
roelofOP14mo ago
you mean on line 50
ἔρως
ἔρως14mo ago
line 50 and 63
roelof
roelofOP14mo ago
we used send_json_errors instead of send_json
ἔρως
ἔρως14mo ago
yup and you're trying to send an error on line 63, right?
roelof
roelofOP14mo ago
yep no change
ἔρως
ἔρως14mo ago
not for you
roelof
roelofOP14mo ago
still see a message that the email is send in green
ἔρως
ἔρως14mo ago
you aren't reaching that part of the code
roelof
roelofOP14mo ago
you are right Right mow he email is send
ἔρως
ἔρως14mo ago
remove the ! and see if you get an error
roelof
roelofOP14mo ago
have to think how to make it work that the email is not send yep, I see a message that the email is not send as expected
ἔρως
ἔρως14mo ago
if you see it, it means that it isn't breaking the javascript
roelof
roelofOP14mo ago
I agree now the annoying part why the spinner is not working ? After dinner/supper
ἔρως
ἔρως14mo ago
push it
roelof
roelofOP14mo ago
oops, and we introduced another problem when success I see this error message in the console :
Uncaught (in promise) TypeError: backend_response.data is undefined
Uncaught (in promise) TypeError: backend_response.data is undefined
put the front end back validation back and the ! pushed all the code
ἔρως
ἔρως14mo ago
alright you do have a bug
ἔρως
ἔρως14mo ago
that line
roelof
roelofOP14mo ago
I see it put the ! on the wrong place
ἔρως
ἔρως14mo ago
yes, you put it there when it should be nowhere
roelof
roelofOP14mo ago
oke have to think well where it should be
ἔρως
ἔρως14mo ago
you shouldn't be you dont need it
roelof
roelofOP14mo ago
oke and the mail sending is working again
ἔρως
ἔρως14mo ago
yup, that should be everything
roelof
roelofOP14mo ago
oke one problem left I still wonder why I do not see the spinner when the button is disabled Still thinking that it is a css problem but I could be false
ἔρως
ἔρως14mo ago
i know why it's happening
roelof
roelofOP14mo ago
im not
ἔρως
ἔρως14mo ago
roelof
roelofOP14mo ago
oke thanks I see now the spinner but a litle time But that is oke so call it a day and ship it
ἔρως
ἔρως14mo ago
that's because the response is way too fast try artificially slowing it down
roelof
roelofOP14mo ago
exactly
ἔρως
ἔρως14mo ago
firefox has network tools for it
roelof
roelofOP14mo ago
but like I said I see it so it is oke
ἔρως
ἔρως14mo ago
you should be super close to being done
roelof
roelofOP14mo ago
So ship it ? pfff. super close it not good just as you said im super close with some code 😢
ἔρως
ἔρως14mo ago
you need to do just some tidying up and you're done
roelof
roelofOP14mo ago
oke where do I do the tyding up I will that do tomorrow , almost time for a quiz on tv that I like to watch and try it myself
ἔρως
ἔρως14mo ago
well, php and css need some tidying, that's mostly it
roelof
roelofOP14mo ago
that is almost 90% of my code ;'(
ἔρως
ἔρως14mo ago
it's just small stuff
roelof
roelofOP14mo ago
oke, tell it and I Will do it or in the commercial breaks or tomorrow
ἔρως
ἔρως14mo ago
just proper error messages, proper error codes better formatting of your code just a few small things in css, like stuff that doesn't belong there and a minified version of the css and javascript the error messages could use a touch you have non-translatable strings, which is bad just small stuff this is usually done by a compiler step, but that's not for today or tomorrow, so, you can ignore this if you think it isn't needed
roelof
roelofOP14mo ago
oke, no problem to do that tomorrow I think im the whole afternoon and a piece of the evening online formatting is already done by vs code
roelof
roelofOP14mo ago
done and done schould there also not be a </html> then in the email.php ?
ἔρως
ἔρως14mo ago
you have to write valud html4 code that means the body, title, html, doctype all those have to be present and the text on line 63 doesn't belong there the indentation on that file is ... artistical
roelof
roelofOP14mo ago
line 63 of the email.php ?
ἔρως
ἔρως14mo ago
yes the last line
roelof
roelofOP14mo ago
is deleted and alll the other is added and what is wrong with the identition I use always 4 speces or a tab
ἔρως
ἔρως14mo ago
that's what's wrong use tab just tab
roelof
roelofOP14mo ago
oke, Then I have to look where I use spaces oke, if I did things well then it is all tabs now I thought I deleted everything I do not need in the css file
ἔρως
ἔρως14mo ago
the file is fine for now
roelof
roelofOP14mo ago
oke surprised me because you said :
just a few small things in css, like stuff that doesn't belong there
just a few small things in css, like stuff that doesn't belong there
ἔρως
ἔρως14mo ago
line 1 to 10 is forcing the font for all the document and line 12 to 23 exist in most resets, but you can limit it to just the form
roelof
roelofOP14mo ago
and what is wrong with forcing a font I use it all the time with front end mentors challenges oke, but I do not use a reset here Can we do the translation of the error mesages in the WP_errror.add part of at another place ? and minifyiung i never did so I have to google how to do that
ἔρως
ἔρως14mo ago
you must translate there
roelof
roelofOP14mo ago
oke, and how about the css ? GN
ἔρως
ἔρως14mo ago
for the css, the 2 changes ive told should be enough goodnight
roelof
roelofOP14mo ago
even without a reset ? if I delete the two then I see the spinner the whole time Hmm, even with that part I see the spinner so still somewhere a problem in the css 😢 and the translation part of the error message like this :
error_messages.push({(__,'Subject has to be more then 3 characters')});
error_messages.push({(__,'Subject has to be more then 3 characters')});
? and should we also not translate and escape this part of the email
<p style="color:#455056; font-size:15px;line-height:24px; margin:20px;">
subject : <?= $data['subject'] ?> <br />
email: <?= $data['email'] ?> <br />
message: <?= $data['message'] ?> <br />
</p>

<p style="color:#455056; font-size:15px;line-height:24px; margin:20px;">
subject : <?= $data['subject'] ?> <br />
email: <?= $data['email'] ?> <br />
message: <?= $data['message'] ?> <br />
</p>

ἔρως
ἔρως14mo ago
the __ is a function
roelof
roelofOP14mo ago
oke
ἔρως
ἔρως14mo ago
also, it is a php function, not javascript yes, you should translate this too
roelof
roelofOP14mo ago
oke, im a little confused how to make that __ part work but can we first make the spinner work right I see it now also when opening the site
roelof
roelofOP14mo ago
No description
roelof
roelofOP14mo ago
So I think the css is still wrong
ἔρως
ἔρως14mo ago
you have to hide it by default, and then show it with display: inline-block when it's supposed to be visible
roelof
roelofOP14mo ago
still no luck now I do not see the spinner
ἔρως
ἔρως14mo ago
did you push?
roelof
roelofOP14mo ago
did I just
roelof
roelofOP14mo ago
oke, looking better I think my computer is too fast to see the spinner
ἔρως
ἔρως14mo ago
that's likely just throttle the requests
roelof
roelofOP14mo ago
I think and hope iti is allright now
ἔρως
ἔρως14mo ago
push it, and i will tell you
roelof
roelofOP14mo ago
is pushed
ἔρως
ἔρως14mo ago
you didn't add the css to show it still not fixed
roelof
roelofOP14mo ago
I thougt I enabled it here :
button.style = "display: inline-block";
button.style = "display: inline-block";
` in the css and I have to look how to make the key frames work I know I used it before with css
ἔρως
ἔρως14mo ago
button[type="submit"]{background:blue;color:white}button[type="submit"]:disabled{background:lightblue;color:black}button[type="submit"]:disabled::before{content:"";display:inline-block;width:0.75em;height:0.75em;margin-right:0.25em;vertical-align:-0.25em;border:0.25em solid white;border-radius:50%;border-top-color:red;animation:spin 0.75s infinite linear}@keyframes spin{to{transform:rotate(360deg)}}
button[type="submit"]{background:blue;color:white}button[type="submit"]:disabled{background:lightblue;color:black}button[type="submit"]:disabled::before{content:"";display:inline-block;width:0.75em;height:0.75em;margin-right:0.25em;vertical-align:-0.25em;border:0.25em solid white;border-radius:50%;border-top-color:red;animation:spin 0.75s infinite linear}@keyframes spin{to{transform:rotate(360deg)}}
^ this is the compiled/minified version of the css for the spinner format it properly and it will work
roelof
roelofOP14mo ago
That part worked now the internatiolisation of the error messages I have to google how that must look like
roelof
roelofOP14mo ago
yep, but do I really have to escape it after some many checks
roelof
roelofOP14mo ago
stupid js with php
$errors->add("Error," <?php esc_html_e('Subject has to be more then 10 characters', 'mycustomForm') ?>)
$errors->add("Error," <?php esc_html_e('Subject has to be more then 10 characters', 'mycustomForm') ?>)
is now working
ἔρως
ἔρως14mo ago
that is wrong like, really really wrong
roelof
roelofOP14mo ago
I thought
ἔρως
ἔρως14mo ago
it's close, but still wrong
roelof
roelofOP14mo ago
that already again close 😢
ἔρως
ἔρως14mo ago
when sending to javascript, you can just send the raw string no need to encode it that's javascript's responsability
roelof
roelofOP14mo ago
oke I understand that it I need to do it there
ἔρως
ἔρως14mo ago
just use __ instead of esc_html_e
roelof
roelofOP14mo ago
so
$errors->add("Error," <?php __('Subject has to be more then 10 characters', 'mycustomForm') ?>)
$errors->add("Error," <?php __('Subject has to be more then 10 characters', 'mycustomForm') ?>)
?
ἔρως
ἔρως14mo ago
close you're opening a php tag inside php
roelof
roelofOP14mo ago
oke so
$errors->add("Error," __('Subject has to be more then 10 characters', 'mycustomForm'));
$errors->add("Error," __('Subject has to be more then 10 characters', 'mycustomForm'));
ἔρως
ἔρως14mo ago
yup, that's right
roelof
roelofOP14mo ago
nope, compiler is not happy when I do
$errors->add("Error," __('Subject has to be more then 10 characters', 'mycustomForm'));
$errors->add("Error," __('Subject has to be more then 10 characters', 'mycustomForm'));

Unexpected 'Name'. Expected ','.

Unexpected 'Name'. Expected ','.
ἔρως
ἔρως14mo ago
oh, right, i see the mistake look where the comma is
roelof
roelofOP14mo ago
me too and solved it I can do the same witht the email template or can I better use esc_htm_e there ?
ἔρως
ἔρως14mo ago
in the email template you do the same you did in the form but for ALL the text
roelof
roelofOP14mo ago
oke will work on it
ἔρως
ἔρως14mo ago
alright
roelof
roelofOP14mo ago
something like this :
<?php esc_html_e( "subject", "mycustomForm") ?> : <php esc_html_e($data['subject'] , 'mycustomForm')") ?> <br />

<?php esc_html_e( "subject", "mycustomForm") ?> : <php esc_html_e($data['subject'] , 'mycustomForm')") ?> <br />

ἔρως
ἔρως14mo ago
close
roelof
roelofOP14mo ago
😢
ἔρως
ἔρως14mo ago
what you receive from the user has to be encoded into html, but CAN'T be translated and $data is what you receive from the user no what you had before for the $data was fine wait, no, it wasn't fine
roelof
roelofOP14mo ago
no it was not subject is red and that is mostly not good
ἔρως
ἔρως14mo ago
<?= $data['subject'] ?> <-- you had this, which is even scarier
roelof
roelofOP14mo ago
<?php esc_html_e( "subject", "mycustomForm") ?> : <php esc_html($data['subject'] , 'mycustomForm')") ?> <br />
<?php esc_html_e( "subject", "mycustomForm") ?> : <php esc_html($data['subject'] , 'mycustomForm')") ?> <br />
ἔρως
ἔρως14mo ago
close remove the 2nd parameter from esc_html also, don't forget to echo the value
roelof
roelofOP14mo ago
oke
ἔρως
ἔρως14mo ago
that should do it
roelof
roelofOP14mo ago
also done
ἔρως
ἔρως14mo ago
and it works?
roelof
roelofOP14mo ago
css done internatiolisation done I will try now
ἔρως
ἔρως14mo ago
🤞
roelof
roelofOP14mo ago
looks well
roelof
roelofOP14mo ago
No description
roelof
roelofOP14mo ago
seems also to work
roelof
roelofOP14mo ago
No description
ἔρως
ἔρως14mo ago
by the way, you may want to remove the shitty colors i've set for the button
roelof
roelofOP14mo ago
except it looks if message uses another font I do not mind the colors
ἔρως
ἔρως14mo ago
that ugly blue is ugly
roelof
roelofOP14mo ago
oke, I will try to find a better color
ἔρως
ἔρως14mo ago
just remove the colors i added
roelof
roelofOP14mo ago
oke and I try to find out why the font is different on the text fields and text-area I find that ugly
ἔρως
ἔρως14mo ago
just use the font you want
roelof
roelofOP14mo ago
I did but you told me yesterday to get rid of it
ἔρως
ἔρως14mo ago
you can set the font for the messages
roelof
roelofOP14mo ago
yep and then it looks better
roelof
roelofOP14mo ago
No description
roelof
roelofOP14mo ago
pushed the last version and I hope you are happy so no minification on the css and js ?
ἔρως
ἔρως14mo ago
for a small project like this, just for you, don't think it is worth it https://gitlab.com/roelofwobben/short-code-form/-/blob/main/js/myCustomForm.js?ref_type=heads#L92 <-- you forgot the data type https://gitlab.com/roelofwobben/short-code-form/-/blob/main/js/myCustomForm.js?ref_type=heads#L87 <-- why are you setting the button to be an inline block in js?
roelof
roelofOP14mo ago
hmm, have to think about that
roelof
roelofOP14mo ago
moment, I can give a respons so quick for the button we have to set it somewhere to make the spinner work
roelof
roelofOP14mo ago
oke, deleted and for the data-types I have to think which type and how do I tell it in js or phpdoc
ἔρως
ἔρως14mo ago
in jsdoc
roelof
roelofOP14mo ago
This good :
/**
* Create FormData from the form
* type {FormData}
*/
var formdata = new FormData(form);

/**
* Create FormData from the form
* type {FormData}
*/
var formdata = new FormData(form);

ἔρως
ἔρως14mo ago
you forgot the @ it's @type
roelof
roelofOP14mo ago
oke I m thinking what the type is here
/**
* Send the form data to the backend
* @param {FormData} - The form data
* @param {form.action}
*/
/**
* Send the form data to the backend
* @param {FormData} - The form data
* @param {form.action}
*/
ἔρως
ἔρως14mo ago
the 2nd parameter is a string and you're forgetting the names of the parameters
roelof
roelofOP14mo ago
so it looks like this :
/**
* Send the form data to the backend
* @param {FormData} formdata- The form data
* @param {string} form.action - the action of the form
*/
var backend_response = await send_to_backend(formdata, form.action);
/**
* Send the form data to the backend
* @param {FormData} formdata- The form data
* @param {string} form.action - the action of the form
*/
var backend_response = await send_to_backend(formdata, form.action);
ἔρως
ἔρως14mo ago
that comment is in the wrong place
roelof
roelofOP14mo ago
??
ἔρως
ἔρως14mo ago
you're describing a function, but you have it on a variable
roelof
roelofOP14mo ago
oke, I have to think about it Im bad in languages like English, German and so on
ἔρως
ἔρως14mo ago
what you have is almost perfect for the function definition but you're calling the function
roelof
roelofOP14mo ago
I know Im thinking how to describe it better maybe something like
Call to the backend
Call to the backend
`
ἔρως
ἔρως14mo ago
it's fine, you can't describe it any better
roelof
roelofOP14mo ago
not me
ἔρως
ἔρως14mo ago
this is what the function does
roelof
roelofOP14mo ago
changed all you wanted so far
ἔρως
ἔρως14mo ago
show me
roelof
roelofOP14mo ago
pushed it
roelof
roelofOP14mo ago
??? what is then the right place
roelof
roelofOP14mo ago
sorry, still confused
roelof
roelofOP14mo ago
Does the showErrorMessages have a return type ?
ἔρως
ἔρως14mo ago
yes: it returns nothing you can use void for that
roelof
roelofOP14mo ago
oke so the first two have a return type of void ?
ἔρως
ἔρως14mo ago
if they don't return anything, yes
roelof
roelofOP14mo ago
if i did not overlooked something , every change is made I think you are going far and far beyond what the person wanted that made this challenge
ἔρως
ἔρως14mo ago
well, i want you to have good habits if it was just to get it done, we could half-ass it and just move on
roelof
roelofOP14mo ago
I think the author wants it done half-ass but that is also not my way of working as far as I know he or she looks if its works and if so , you get ppints
ἔρως
ἔρως14mo ago
yeah, but if you half-ass everything, you will get the habit of half-assing everything then it's a vicious cicle of half-assery which is hard to break good habits have to be set from the beginning https://gitlab.com/roelofwobben/short-code-form/-/blob/main/js/myCustomForm.js?ref_type=heads#L51 <-- stuff like this: it has mixed quotes you should fix it, and it takes 1 second to do it right the next time
roelof
roelofOP14mo ago
yep, as I see in many wordpress people. They use something like elementor, buy a template, change some colors and call themself wordpress developers
ἔρως
ἔρως14mo ago
but if nobody tells you, you won't know, and will still just flop about on quotes
roelof
roelofOP14mo ago
For me , they are not
ἔρως
ἔρως14mo ago
eh, they are designers at best but it's also important to show you what's a good comment and a bad comment
roelof
roelofOP14mo ago
I think you are a wordpress developer if you can make a template from the beginning or make plugins with php or react from the beginning
ἔρως
ἔρως14mo ago
i agree but you need to start with good habits from the beginning you can't just face-plant into everything
roelof
roelofOP14mo ago
so im still not a wordpress developer , Cannot do all three at the moment
ἔρως
ἔρως14mo ago
you're doing something, so, it counts
roelof
roelofOP14mo ago
but still learning
ἔρως
ἔρως14mo ago
you're not grabbing a theme and changing colors
roelof
roelofOP14mo ago
but any comments on our project nope, at "work" im making a block template from a html template cost a lot of time to learn but also it is fun I say "work" because it is not a payed job but a way for me to get into a normal way of living and gettng help with it sp to overcome my mental problems like aniexity
ἔρως
ἔρως14mo ago
i get what you mean but still, you need good habits from the beginning, and apply them no matter what
roelof
roelofOP14mo ago
i agree ship it or can I change more
ἔρως
ἔρως14mo ago
there's still some things https://gitlab.com/roelofwobben/short-code-form/-/blob/main/templates/email.php?ref_type=heads#L45 <-- the message can have multiple lines
roelof
roelofOP14mo ago
correct
ἔρως
ἔρως14mo ago
when the message is shown there, it won't have multiple lines
roelof
roelofOP14mo ago
oops back to the drawing table
ἔρως
ἔρως14mo ago
either put it inside a <pre> or just run the message through nl2br()
roelof
roelofOP14mo ago
I think after the esc_html ?
ἔρως
ἔρως14mo ago
yup
roelof
roelofOP14mo ago
done
<?php esc_html_e("message", "mycustomForm") ?>: <?= esc_html(nl2br($data['message'])) ?> <br />

<?php esc_html_e("message", "mycustomForm") ?>: <?= esc_html(nl2br($data['message'])) ?> <br />

ἔρως
ἔρως14mo ago
no, the other way around
roelof
roelofOP14mo ago
?? you said after the esc_html ?
ἔρως
ἔρως14mo ago
yes, i was thinking in the order the functions are called they are called right to left
roelof
roelofOP14mo ago
oke
<?php esc_html_e("message", "mycustomForm") ?>: <?= nl2br(esc_html($data['message'])) ?> <br />
<?php esc_html_e("message", "mycustomForm") ?>: <?= nl2br(esc_html($data['message'])) ?> <br />
ἔρως
ἔρως14mo ago
a(b(c())) <-- this will call a after b and b after c
roelof
roelofOP14mo ago
yep, I agree
ἔρως
ἔρως14mo ago
perfect!
roelof
roelofOP14mo ago
lot of fun when you try clojure 🙂 then it looks like (a(b(c())))
ἔρως
ἔρως14mo ago
oh yeah, it's fun
<tr>
<td style="height:20px;">&nbsp;</td>
</tr>
<tr>
<td style="height:80px;">&nbsp;</td>
</tr>
<tr>
<td style="height:20px;">&nbsp;</td>
</tr>
<tr>
<td style="height:80px;">&nbsp;</td>
</tr>
^ any reason why you have 2 rows?
roelof
roelofOP14mo ago
yep, one for the things like "subject" and one for the answer that the user entered
ἔρως
ἔρως14mo ago
but these are empty
roelof
roelofOP14mo ago
oke, I will delete both
ἔρως
ἔρως14mo ago
no no, they are used for spacing but why 2 instead of 1 with 100px height?
roelof
roelofOP14mo ago
no idea I just copied the template and changed what I thought needed to be changed
ἔρως
ἔρως14mo ago
alright
roelof
roelofOP14mo ago
We can try ?
ἔρως
ἔρως14mo ago
sure, try it
roelof
roelofOP14mo ago
After dinner I have to look how I can see the email Laragon has a email catcher but I see then html
ἔρως
ἔρως14mo ago
didn't knew that
roelof
roelofOP14mo ago
nope, I cannot see the email 😦
ἔρως
ἔρως14mo ago
how did you saw the emails before?
roelof
roelofOP14mo ago
nope at my computer outlook is started but no mail to see I wlll try my own mail adress nope, nothing recieved looks like I need a sort of mail server to really see the emails 😦
ἔρως
ἔρως14mo ago
yup, you do how did you sent the other test emails?
roelof
roelofOP14mo ago
I did not use a mail server but found a way to see them and it looking well to me
roelof
roelofOP14mo ago
No description
ἔρως
ἔρως14mo ago
then that's good
roelof
roelofOP14mo ago
another check for shipping it 🙂
ἔρως
ἔρως14mo ago
did you merge? i mean, push
roelof
roelofOP14mo ago
not yer moment pushed
ἔρως
ἔρως14mo ago
roelof
roelofOP14mo ago
trim $POST like this :
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) trim($_POST));
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) trim($_POST));
ἔρως
ἔρως14mo ago
close, but no it's an array, so, you have to use either a foreach loop or an array_map
roelof
roelofOP14mo ago
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) array_map( (item) => trim(item) )($_POST));
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) array_map( (item) => trim(item) )($_POST));
ἔρως
ἔρως14mo ago
i would just use the foreach loop
roelof
roelofOP14mo ago
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) foreach($item as $POST) { trim(item) };
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) foreach($item as $POST) { trim(item) };
ἔρως
ἔρως14mo ago
that's not how you do a foreach
roelof
roelofOP14mo ago
I saw it , compiler is not happy
ἔρως
ἔρως14mo ago
and with good reason
roelof
roelofOP14mo ago
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], foreach($POST as $item) { trim(item) };
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], foreach($POST as $item) { trim(item) };
compiler is not happy with my foreach
ἔρως
ἔρως14mo ago
did you checked the documentation?
ἔρως
ἔρως14mo ago
and what's saying there?
roelof
roelofOP14mo ago
I have to use unset Wonder if I not bring this outside
ἔρως
ἔρως14mo ago
yes, it has to be outside
roelof
roelofOP14mo ago
compiler is happy now
foreach($item as $POST) {
$item = trim($item);
}

unset($item );

$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) $_POST);
foreach($item as $POST) {
$item = trim($item);
}

unset($item );

$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) $_POST);
ἔρως
ἔρως14mo ago
no, the other way around and you loop the data, not the post
roelof
roelofOP14mo ago
I know but then I see this one :
roelof
roelofOP14mo ago
No description
roelof
roelofOP14mo ago
Does not make any sense to me Compiler thinks both are a array so it looks i need two foreach loops then wit this code the compiler is happy
foreach($_POST as $items) {
foreach($items as $item) {
$item = trim($item);
}
}

unset($item );

$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) $_POST);
foreach($_POST as $items) {
foreach($items as $item) {
$item = trim($item);
}
}

unset($item );

$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) $_POST);
ἔρως
ἔρως14mo ago
the compiler is happy but it doesn't do anything
roelof
roelofOP14mo ago
😢
ἔρως
ἔρως14mo ago
as i said, you have to loop through $data
roelof
roelofOP14mo ago
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) $_POST);

foreach ($data as $item) {
$item = trim($item);
}

unset($item);
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) $_POST);

foreach ($data as $item) {
$item = trim($item);
}

unset($item);
ἔρως
ἔρως14mo ago
nearly there but there's a problem: the data will be lost that's not a pointer variable: it's just a copy and another problem: you could have an array instead of a string
roelof
roelofOP14mo ago
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) $_POST);

foreach ($data as &$item) {
$item = trim($item);
}

unset($item);
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) $_POST);

foreach ($data as &$item) {
$item = trim($item);
}

unset($item);
How so can it be a string
ἔρως
ἔρως14mo ago
you can send arrays over post
roelof
roelofOP14mo ago
oke but still I do not get your point
ἔρως
ἔρως14mo ago
my point is that your code will stop working and since you're only handling specific keys, you can do something cool i will show you
$data = ["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""];

foreach (array_keys($data) as $key) {
if(empty($_POST[$key]) || !is_string($_POST[$key])) {
continue;
}

$data[$key] = trim($_POST[$key]);
}
$data = ["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""];

foreach (array_keys($data) as $key) {
if(empty($_POST[$key]) || !is_string($_POST[$key])) {
continue;
}

$data[$key] = trim($_POST[$key]);
}
so, instead of looping the values and stuff, you loop the keys and only overwrite the value if it exists and is a string
roelof
roelofOP14mo ago
oke, I get the feeling that you ask me beyond I ever learned PHP or developing
ἔρως
ἔρως14mo ago
i did this is something beyound that's why i wrote it for you
roelof
roelofOP14mo ago
I get the feeling that I again hit a wall I cannot go beyond it
ἔρως
ἔρως14mo ago
that's why i showed how to jump over it
roelof
roelofOP14mo ago
and that is not a nice feeling and that is why I quit many many languages
ἔρως
ἔρως14mo ago
this is a personal preference thing, as this can be done in many ways
roelof
roelofOP14mo ago
and I still need the unset ?
ἔρως
ἔρως14mo ago
it doesn't matter
roelof
roelofOP14mo ago
oke Now I see what you mean if its empty or not a string do nothing with it otherwise trim it
ἔρως
ἔρως14mo ago
yup, and set it to the data array
roelof
roelofOP14mo ago
yep, that part I also forgot
ἔρως
ἔρως14mo ago
but also, it only checks the keys you're interested in
roelof
roelofOP14mo ago
but also I could not do it in my code 🙂 anymore to learn you are not easy to make happy when its about coding I love it and sometimes I hate it
ἔρως
ἔρως14mo ago
as i said, i have to pass good habits to you, and that also means holding myself to an higher standard
roelof
roelofOP14mo ago
I like it, deep in my heart I want to do it good But I have to keep myself from doing things perfect
ἔρως
ἔρως14mo ago
when receiving user input, nothing short of "perfect" is acceptable
roelof
roelofOP14mo ago
Even if I know this plugin will never be used in real
ἔρως
ἔρως14mo ago
that's why im being so nitpicky in that specific area
roelof
roelofOP14mo ago
but maybe in the far future it will
ἔρως
ἔρως14mo ago
maybe, but you still need to learn to never trust ANYTHING that comes from users
roelof
roelofOP14mo ago
I know that already and that there are users that can mess with the sending to the backend That is why I wanted client and backend validation
ἔρως
ἔρως14mo ago
yup, and you have it now
roelof
roelofOP14mo ago
and did the nonce check
ἔρως
ἔρως14mo ago
as you should
roelof
roelofOP14mo ago
so a user cannot send data from outside to my site
ἔρως
ἔρως14mo ago
they still will try, but it's fine
roelof
roelofOP14mo ago
So I think I did there a good job
ἔρως
ἔρως14mo ago
you gotta protect your butt
roelof
roelofOP14mo ago
yep
ἔρως
ἔρως14mo ago
you did
roelof
roelofOP14mo ago
and 100 % protection is I think impossible so ship the project or is there more to learn ?
roelof
roelofOP14mo ago
oke, I have to google where there must be a domain
ἔρως
ἔρως14mo ago
2nd argument also, if you put your mouse hover a function, it shows information about it
roelof
roelofOP14mo ago
your sure there must be a domain array_replace gives me array_replace(array, replace_array)
roelof
roelofOP14mo ago
oke but I do not use __ there
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) $_POST);
$data = array_replace(["subject" => "", "email" => "", "message" => "", "_wpnonce" => ""], (array) $_POST);
ἔρως
ἔρως14mo ago
if (!is_email($data['email'])) {
$errors->add("Error", __("Please provide a valid email"));
}

if (mb_strlen($data['message']) < 2) {
$errors->add('Error', __("message has to be more then 2 characters"));
}

if (!wp_verify_nonce($data['_wpnonce'], 'submit_contact_form')) {
$errors->add('Error', __('Form is messed up'));
}
if (!is_email($data['email'])) {
$errors->add("Error", __("Please provide a valid email"));
}

if (mb_strlen($data['message']) < 2) {
$errors->add('Error', __("message has to be more then 2 characters"));
}

if (!wp_verify_nonce($data['_wpnonce'], 'submit_contact_form')) {
$errors->add('Error', __('Form is messed up'));
}
you don't?
roelof
roelofOP14mo ago
but that is not line 31 🙂 but you are right
ἔρως
ἔρως14mo ago
🤣
roelof
roelofOP14mo ago
changed it and saw that I forget the internationalisation on one of the items so all changed
ἔρως
ἔρως14mo ago
the bad request wasnt translated too?
roelof
roelofOP14mo ago
yep we have then a little problem on customForm.js
ἔρως
ἔρως14mo ago
what is it?
roelof
roelofOP14mo ago
I do this :

showErrorMessages(["Mail has been send"], "success");

showErrorMessages(["Mail has been send"], "success");
that is never translated or I must there mix php and js there ?
ἔρως
ἔρως14mo ago
you're right, it isn't translated but no, you don't
roelof
roelofOP14mo ago
I hope there is a way
ἔρως
ἔρως14mo ago
there is
roelof
roelofOP14mo ago
would be a pity if all is translated and that not
roelof
roelofOP14mo ago
oke, I will read that when my head is more clear
ἔρως
ἔρως14mo ago
it's a heavy bunch of reading
roelof
roelofOP14mo ago
but if I read it fast this could be a solution
// Register the script
wp_register_script( 'some_handle', 'path/to/myscript.js' );

// Localize the script with new data
$translation_array = array(
'some_string' => __( 'Some string to translate', 'plugin-domain' ),
'a_value' => '10'
);
wp_localize_script( 'some_handle', 'object_name', $translation_array );

// Enqueued script with localized data.
wp_enqueue_script( 'some_handle' );
// Register the script
wp_register_script( 'some_handle', 'path/to/myscript.js' );

// Localize the script with new data
$translation_array = array(
'some_string' => __( 'Some string to translate', 'plugin-domain' ),
'a_value' => '10'
);
wp_localize_script( 'some_handle', 'object_name', $translation_array );

// Enqueued script with localized data.
wp_enqueue_script( 'some_handle' );

alert( object_name.some_string );

alert( object_name.some_string );
ἔρως
ἔρως14mo ago
basically, yes
roelof
roelofOP14mo ago
I will play with it tomorrow afternoon if I have energy Tomrrow morning im "fighting" with gutenburg to make my first template Thanks again with all your patience and I hope im still a 6 - 7 student
ἔρως
ἔρως14mo ago
don't worry, you are
roelof
roelofOP14mo ago
thanks
ἔρως
ἔρως14mo ago
just rest and we can try it tomorrow
roelof
roelofOP14mo ago
I seems the world is more confident about what I can then me I doubt a a lot about myself lately Time to hit the shower
ἔρως
ἔρως14mo ago
we all doubt ourselves sometimes the trick is to keep going
roelof
roelofOP14mo ago
oke and I saw that I have a lot to learn and sometimes I have to work slower instead of faster what is in my character to wanted to go fast
ἔρως
ἔρως14mo ago
well, if you go fast without knowing what you're doing, you're just making a mess
roelof
roelofOP14mo ago
yep and forget things like the domain
ἔρως
ἔρως14mo ago
exactly
roelof
roelofOP14mo ago
and learning cost time
ἔρως
ἔρως14mo ago
you know what costs even more time?
roelof
roelofOP14mo ago
a lort of time and trail and error go fast and make a lot of bugs that cannot be easilky found
ἔρως
ἔρως14mo ago
or make your code into such a mess than you have to discard it and start over this is why the good habits are important
roelof
roelofOP14mo ago
I had that a lot but that is also a part of learning know when youy can better start all over again because you took a wrong path to solve a problem
ἔρως
ἔρως14mo ago
that happens to everybody but a good developer will know what to do, if possible
roelof
roelofOP14mo ago
tomorrow make the last translation work and hopefully then done We worked I think 2 weeks now on it
ἔρως
ἔρως14mo ago
something around that and you though it was a tiny thing?
roelof
roelofOP14mo ago
I thougth yes because it is a junoir challenge according to the maker
ἔρως
ἔρως14mo ago
but you werent a junior: you were a beginner which is fine
roelof
roelofOP14mo ago
i m a super beginner which is maybe to eager to learn
ἔρως
ἔρως14mo ago
all you see is the finish line, but you don't see where you're putting your feet
roelof
roelofOP14mo ago
🙂 so in other words, I look at the goal, and forget to enjoy the ride
ἔρως
ἔρως14mo ago
you just go for it you're blinded by the finish line
roelof
roelofOP14mo ago
I know, that is why not trying for 4 years to go back to a job Is one of the working points for me now
ἔρως
ἔρως14mo ago
yup, but you will get there, so, don't worry
roelof
roelofOP14mo ago
we see what the future will bring
ἔρως
ἔρως14mo ago
we will see but now, just rest
roelof
roelofOP14mo ago
yes boss
ἔρως
ἔρως14mo ago
🫡
roelof
roelofOP14mo ago
LOL
ἔρως
ἔρως14mo ago
but seriously, resting is important
roelof
roelofOP14mo ago
I know I learned it the hard way and still learning
ἔρως
ἔρως14mo ago
i know
roelof
roelofOP14mo ago
Can you help me why the message is not found latest code is on the repo I see a green area but the text seems to be missing works again Now I can als translate all the front end validation errors 🙂 if it is right all messages are translated new code is pushed
ἔρως
ἔρως14mo ago
i will check it later on, after work
roelof
roelofOP14mo ago
all oke hopefully everything is allright
ἔρως
ἔρως14mo ago
i hope so too
roelof
roelofOP14mo ago
How was your working day ?
ἔρως
ἔρως14mo ago
still have 45 minutes to go and im way too tired to focus ... and the neighbour is drilling right next to me ...
roelof
roelofOP14mo ago
not good i was not so productibe today Had to delete a part of the template and start over again Hopefully Friday I Can make a great loop im now I think 20 hours and still 50% of the front page but learn a lot
ἔρως
ἔρως14mo ago
learning a lot is important its never a waste of time to learn something
roelof
roelofOP14mo ago
yep and like I said learning cost time and a lot of trying , fail and try again with sometimes some help
ἔρως
ἔρως14mo ago
yup, but that is all part of the process
roelof
roelofOP14mo ago
exactly Take good care of yourself and I see when you have time to see if our project is up to standard I say this because I think you are tired because you said you can concetrate
ἔρως
ἔρως14mo ago
i am very tired, but i was doing, basically, grunt work
roelof
roelofOP14mo ago
o , sounds not good
ἔρως
ἔρως14mo ago
it's not https://gitlab.com/roelofwobben/short-code-form/-/blob/main/js/myCustomForm.js?ref_type=heads#L91 <-- nitpick: this is indented with spaces https://gitlab.com/roelofwobben/short-code-form/-/blob/main/js/myCustomForm.js?ref_type=heads#L44 <-- this function could be named showMessages instead https://gitlab.com/roelofwobben/short-code-form/-/blob/main/js/myCustomForm.js?ref_type=heads#L41 <-- you forgot the status param but for this, you can do something better: call it type and then only use lowercase letters
if (status == 'success') {
errorDiv.classList.add('success');
} else {
errorDiv.classList.add('error')
}
if (status == 'success') {
errorDiv.classList.add('success');
} else {
errorDiv.classList.add('error')
}
then, instead of this, it can be just errorDiv.classList.add(type); https://gitlab.com/roelofwobben/short-code-form/-/blob/main/js/myCustomForm.js?ref_type=heads#L64 <-- this is absolutely useless, as errorDivElement is just errorDiv, but you go the long way to get it everywhere you use errorDivElement. just use errorDiv
roelof
roelofOP14mo ago
With the status im not complete sure it can be replaced The renaming is not working then we also have to rename this one :
const errorDiv = document.createElement('div');
const errorDiv = document.createElement('div');
a few lines earlier
ἔρως
ἔρως14mo ago
no, do not rename this! at least, not yet first, it has to work
roelof
roelofOP14mo ago
Then I cannot rename ErrorDivElement to ErrorDiv because it already exist
ἔρως
ἔρως14mo ago
it's not a rename it's a delete that constant shouldn't exist at all this is what you're doing: 1- create element 2- add to the form 3- fetch the element created in point 1 using a css selector
roelof
roelofOP14mo ago
oke, but how do i then make a difference between a error and a success div Both does not exist at that moment
ἔρως
ἔρως14mo ago
easy: you create a div, add the class in the way i told you and ... done that's it
roelof
roelofOP14mo ago
oke , so add the class to the ErrorDiv but then we can delete this whole part
errorDiv.classList.add(type);

// Append the error div to the user-feedback div
userFeedbackDiv.appendChild(errorDiv);

/**
* Select the error div
* @type {HTMLDivElement}
*/
const errorDiv = form.querySelector('.error') || form.querySelector('.success');
errorDiv.classList.add(type);

// Append the error div to the user-feedback div
userFeedbackDiv.appendChild(errorDiv);

/**
* Select the error div
* @type {HTMLDivElement}
*/
const errorDiv = form.querySelector('.error') || form.querySelector('.success');
`
ἔρως
ἔρως14mo ago
/**
* Select the error div
* @type {HTMLDivElement}
*/
const errorDiv = form.querySelector('.error') || form.querySelector('.success');
/**
* Select the error div
* @type {HTMLDivElement}
*/
const errorDiv = form.querySelector('.error') || form.querySelector('.success');
^ just this
roelof
roelofOP14mo ago
oke, I think I finally undertand what you mean
ἔρως
ἔρως14mo ago
basically, you're fetching an element that you already have in a variable so, the fetching is extra useless work
roelof
roelofOP14mo ago
yep, when I do what you said it still working
roelof
roelofOP14mo ago
this a a good example where im stuck in my own thinking pattern yep, I see it
ἔρως
ἔρως14mo ago
move it after the forEach the last line in the function
roelof
roelofOP14mo ago
done
ἔρως
ἔρως14mo ago
push it
roelof
roelofOP14mo ago
done something is not right when I do a empty form I see the spinner
roelof
roelofOP14mo ago
and I see this mesage in the console:
Uncaught (in promise) ReferenceError: errorParagraph is not defined
Uncaught (in promise) ReferenceError: errorParagraph is not defined
ἔρως
ἔρως14mo ago
you need to swap line 68 and 69 then line 56 goes between line 69 and 70
roelof
roelofOP14mo ago
ke yep, working again
ἔρως
ἔρως14mo ago
push it!
roelof
roelofOP14mo ago
oke done
ἔρως
ἔρως14mo ago
https://gitlab.com/roelofwobben/short-code-form/-/blob/main/js/myCustomForm.js?ref_type=heads#L55 <-- this comment is useless https://gitlab.com/roelofwobben/short-code-form/-/blob/main/js/myCustomForm.js?ref_type=heads#L59 <-- this comment is plain wrong: you're receiving a paragraph and not a div you also don't need the other comments inside the forEach
roelof
roelofOP14mo ago
moment, I do not understand the last change why the comment is wrong I see it , I think
ἔρως
ἔρως14mo ago
it's not wrong it's just not needed
roelof
roelofOP14mo ago
is this better :
/**
* @type {Paragraph}
*/
const errorParagraph = document.createElement('p');
/**
* @type {Paragraph}
*/
const errorParagraph = document.createElement('p');
ἔρως
ἔρως14mo ago
no that's worse
roelof
roelofOP14mo ago
😢
ἔρως
ἔρως14mo ago
HTMLParagraphElement document.createElement('p').constructor.name <-- if you need to know the type, you can do something like this, in the browser console
roelof
roelofOP14mo ago
thanks also changed but I thought you were tired and wanted some rest today instead of working with me and pushed
ἔρως
ἔρως14mo ago
I'm helping you just a little i can rest after https://gitlab.com/roelofwobben/short-code-form/-/blob/main/js/myCustomForm.js?ref_type=heads#L73 <-- you forgot the e param with type SubmitEvent https://gitlab.com/roelofwobben/short-code-form/-/blob/main/js/myCustomForm.js?ref_type=heads#L17 <-- and here, it's also missing the HTMLFormElement type for the form variable
roelof
roelofOP14mo ago
this good :
* @params {Event} e - the event
* @params {Event} e - the event
ἔρως
ἔρως14mo ago
wront type, and the message can be skipped you know, the description also, don't add - to the description it will render as 2 dashes
roelof
roelofOP14mo ago
oke, I was also thinking of MouseEvent but it could also be a KeyBoardEvent
ἔρως
ἔρως14mo ago
it's neither it's what i told there it's not mouse or keyboard, as there's other ways to trigger the event
roelof
roelofOP14mo ago
sorry, missedit
ἔρως
ἔρως14mo ago
that's alright just remember that there are other input devices, like touch screens, joysticks, enter key in a virtual keyboard and even just "touching" something in vr
roelof
roelofOP14mo ago
exaclty
ἔρως
ἔρως14mo ago
that's why it's not a mouse or a keyboard event makes sense?
roelof
roelofOP14mo ago
totally make sense I cannot test if my cod work with touch screens and so on but that is oke
ἔρως
ἔρως14mo ago
if it uses the submit event, then yes
roelof
roelofOP14mo ago
and pused pushed
roelof
roelofOP14mo ago
changed
ἔρως
ἔρως14mo ago
but check it everywhere, as you did this mistake in multiple times
roelof
roelofOP14mo ago
oke changed
ἔρως
ἔρως14mo ago
https://jsdoc.app/tags-returns <-- the return types have to be {between braces} and that goes for {void} too
roelof
roelofOP14mo ago
changed but does the compiler or whatever anything with the jsdoc ? See never messages that the type is missing or not right
ἔρως
ἔρως14mo ago
the ide does, and will show weird squigly lines if you don't do it right also, it helps you to catch errors imagine you try to use e.submitter on a click event that doesn't make sense, as the e.submitter is only available in a submit event and vscode will inform you of it
roelof
roelofOP14mo ago
oke, All cases I do not see the squigly lines
ἔρως
ἔρως14mo ago
yes, because (by default) it assumes it's any, which accepts any value
roelof
roelofOP14mo ago
oke
ἔρως
ἔρως14mo ago
that's why you should write the data types since javascript doesn't let you set any
roelof
roelofOP14mo ago
yep, any is not good
ἔρως
ἔρως14mo ago
it's good, sometimes but when you expect a list of forms, it helps you to say that you're treating it as a form
roelof
roelofOP14mo ago
last version is pushed
roelof
roelofOP14mo ago
now im suprised yesterday you told it allright
roelof
roelofOP14mo ago
we worked on that part
ἔρως
ἔρως14mo ago
i actually told you that it was wrong where it is you can check it but don't try to fix it yet https://gitlab.com/roelofwobben/short-code-form/-/blob/main/js/myCustomForm.js?ref_type=heads#L8 <-- you're missing the return value here
roelof
roelofOP14mo ago
This better :
/**
* Function to send form data to the backend
* @param {FormData} data - The form data
* @param {string} url - The backend URL
* @return {Promise}
*/
/**
* Function to send form data to the backend
* @param {FormData} data - The form data
* @param {string} url - The backend URL
* @return {Promise}
*/
`
ἔρως
ἔρως14mo ago
:/ almost you're right, it returns a promise but, it also doesn't return a promise
roelof
roelofOP14mo ago
?? wait , i can also return a object
ἔρως
ἔρως14mo ago
everything is an object it's like any, but more useless
roelof
roelofOP14mo ago
oke, I mean a object that holds a success or error messages
ἔρως
ἔρως14mo ago
yup, but wait a bit
roelof
roelofOP14mo ago
I do that I just try to tell you what im thinking I try to figure out the also doesn't return a promise part
ἔρως
ἔρως14mo ago
well, it's an async function, so, automatically returns a promise but also, it's returning a json object
roelof
roelofOP14mo ago
so it needs to be this :
/**
* Call to the backend
* @param {FormData} data - The form data
* @param {string} url - The backend URL
* @return {JSON}
*/
async function send_to_backend(data, url) {
var response = await fetch(url, {
method: 'POST',
body: data
})
return response.json();
}
/**
* Call to the backend
* @param {FormData} data - The form data
* @param {string} url - The backend URL
* @return {JSON}
*/
async function send_to_backend(data, url) {
var response = await fetch(url, {
method: 'POST',
body: data
})
return response.json();
}
`
ἔρως
ἔρως14mo ago
no put the promise back
roelof
roelofOP14mo ago
oke sometimes it is very hard to understand what you exactly wants
ἔρως
ἔρως14mo ago
because this is a very complex and (very terrible) area of jsdoc
roelof
roelofOP14mo ago
oke do you mean promise<json> ??
ἔρως
ἔρως14mo ago
that's very close, but no
roelof
roelofOP14mo ago
because it is now Promise<any>
ἔρως
ἔρως14mo ago
you actually have to create your own type, which is very ... irritating
roelof
roelofOP14mo ago
hopefully it is not down in the rabbits hole
ἔρως
ἔρως14mo ago
it is 😦 but i will write it for you
roelof
roelofOP14mo ago
Can I run ? 😛
ἔρως
ἔρως14mo ago
you can run and hide, but you can't escape 🤣
roelof
roelofOP14mo ago
chips too late just like my own deamons. I could run for more then 50 years but thet have all catch me ':'(
ἔρως
ἔρως14mo ago
it's fine, im just testing it
roelof
roelofOP14mo ago
np
ἔρως
ἔρως14mo ago
/**
* @typedef {Object} ServerError
* @property {String} code
* @property {String} message
*
* @typedef {Object} ServerResponse
* @property {Boolean} success
* @property {?Array.<ServerError>} data
*/
/**
* @typedef {Object} ServerError
* @property {String} code
* @property {String} message
*
* @typedef {Object} ServerResponse
* @property {Boolean} success
* @property {?Array.<ServerError>} data
*/
here's the disgusting mess this goes on the very very very top of the js file /** @type {ServerResponse} */ <-- this is the type you use and receive
roelof
roelofOP14mo ago
so like this :
/**
* Call to the backend
* @typedef {Object} ServerError
* @property {String} code
* @property {String} message
*
* @typedef {Object} ServerResponse
* @property {Boolean} success
* @property {?Array.<ServerError>} data
*/
var all_forms = document.querySelectorAll('[data-shortcode="contact_form"] > form');

/**
* Call to the backend
* @param {FormData} data - The form data
* @param {string} url - The backend URL
* @return {Promise}
*/
async function send_to_backend(data, url) {
var response = await fetch(url, {
method: 'POST',
body: data
})
return response.json();
}
/**
* Call to the backend
* @typedef {Object} ServerError
* @property {String} code
* @property {String} message
*
* @typedef {Object} ServerResponse
* @property {Boolean} success
* @property {?Array.<ServerError>} data
*/
var all_forms = document.querySelectorAll('[data-shortcode="contact_form"] > form');

/**
* Call to the backend
* @param {FormData} data - The form data
* @param {string} url - The backend URL
* @return {Promise}
*/
async function send_to_backend(data, url) {
var response = await fetch(url, {
method: 'POST',
body: data
})
return response.json();
}
ἔρως
ἔρως14mo ago
you removed the comment for all_forms?
roelof
roelofOP14mo ago
I did
ἔρως
ἔρως14mo ago
you shouldn't
roelof
roelofOP14mo ago
I thought there were never was a comment
ἔρως
ἔρως14mo ago
i though so too that has a type of NodeListOf<HTMLFormElement> you have to separate it with 2-3 new lines
roelof
roelofOP14mo ago

/**
* @typedef {Object} ServerError
* @property {String} code
* @property {String} message
*
* @typedef {Object} ServerResponse
* @property {Boolean} success
* @property {?Array.<ServerError>} data
*/



/**
* @type NodeListOf<HTMLFormElement>
*/
var all_forms = document.querySelectorAll('[data-shortcode="contact_form"] > form');

/**
* Call to the backend
* @param {FormData} data - The form data
* @param {string} url - The backend URL
* @return {Promise}
*/
async function send_to_backend(data, url) {
var response = await fetch(url, {
method: 'POST',
body: data
})
return response.json();
}

/**
* @typedef {Object} ServerError
* @property {String} code
* @property {String} message
*
* @typedef {Object} ServerResponse
* @property {Boolean} success
* @property {?Array.<ServerError>} data
*/



/**
* @type NodeListOf<HTMLFormElement>
*/
var all_forms = document.querySelectorAll('[data-shortcode="contact_form"] > form');

/**
* Call to the backend
* @param {FormData} data - The form data
* @param {string} url - The backend URL
* @return {Promise}
*/
async function send_to_backend(data, url) {
var response = await fetch(url, {
method: 'POST',
body: data
})
return response.json();
}
ἔρως
ἔρως14mo ago
now, the function returns a Promise<ServerResponse>
roelof
roelofOP14mo ago
all changed
ἔρως
ἔρως14mo ago
wait, you're not done
roelof
roelofOP14mo ago
Can we quit for today I have to send a important document for my daugther
ἔρως
ἔρως14mo ago
roelof
roelofOP14mo ago
is that really needed
No description
ἔρως
ἔρως14mo ago
it's good to be explicit
roelof
roelofOP14mo ago
it looks the compiler has already figure that out
ἔρως
ἔρως14mo ago
it did, yes but the code is also read by humans and humans won't know, at a glance, what's the type of it
roelof
roelofOP14mo ago
oke and also add the formdata and form.action ?
ἔρως
ἔρως14mo ago
no that has nothing to do with that
roelof
roelofOP14mo ago
oke
ἔρως
ἔρως14mo ago
you're documenting the type that the variable will have
roelof
roelofOP14mo ago
so it have to look like this:
/**
* @return ServerResponse
*/
var backend_response = await send_to_backend(formdata, form.action);
/**
* @return ServerResponse
*/
var backend_response = await send_to_backend(formdata, form.action);
ἔρως
ἔρως14mo ago
perfect wait no @type {ServerResponse} types between {braces} if i didn't do it, fix it tomorrow
roelof
roelofOP14mo ago
I did that already
ἔρως
ἔρως14mo ago
nice
roelof
roelofOP14mo ago
and pushed the last version
roelof
roelofOP14mo ago
also changed
ἔρως
ἔρως14mo ago
then it's done
roelof
roelofOP14mo ago
and pushed oke, then I can submit it and hopefully the author of the challenge aprroves it also I expect a new challenge somewhere this week and the results of these challenges
ἔρως
ἔρως14mo ago
there's stuff that can be improved, but, honestly it's a rabbit hole with no end
roelof
roelofOP14mo ago
yep, I know
ἔρως
ἔρως14mo ago
test it and, if it works, ship it
roelof
roelofOP14mo ago
the problem of developing I can always be improved but the "client" wants to see a result somewhere
ἔρως
ἔρως14mo ago
then test to see if it is working, and then push
roelof
roelofOP14mo ago
it seems to work a empty form I see error messages and a valid form I see the mail is send message so I ship it
ἔρως
ἔρως14mo ago
ship it
roelof
roelofOP14mo ago
Thanks
ἔρως
ἔρως14mo ago
you're welcome
roelof
roelofOP14mo ago
and I hope I may ask you again if there is a php challenge I hope there will be a sort of template challenge 🙂
ἔρως
ἔρως14mo ago
not gonna lie, this has been very draining, as i hate teaching i like to help, don't get me wrong
roelof
roelofOP14mo ago
For both is has been draining GN and again thanks
ἔρως
ἔρως14mo ago
it has but, you first should try to implement it yourself instead of grabbing someone else's half-assed work as a base
roelof
roelofOP14mo ago
maybe I will afte some days rest
ἔρως
ἔρως14mo ago
you need it
roelof
roelofOP14mo ago
maybe the next challenge is again a php problem
ἔρως
ἔρως14mo ago
hopefully
roelof
roelofOP14mo ago
then I could use this a a sort of example could also be a react problem or a php template problem or a Gutenberg template problem we see you can do a lot of things with wordpress
ἔρως
ἔρως14mo ago
we will see, but for now just rest
roelof
roelofOP14mo ago
yep a few busy days the next few days so no developing
ἔρως
ἔρως14mo ago
good, you need to rest a little
roelof
roelofOP14mo ago
no. rest a lot
ἔρως
ἔρως14mo ago
that's even better
roelof
roelofOP14mo ago
and also rest a lot because of the grunding work
ἔρως
ἔρως14mo ago
i will rest at least 8 hours
roelof
roelofOP14mo ago
I hope I can rest so long the last three days im always awake in the middle of the night with some stress in my mind or body
ἔρως
ἔρως14mo ago
now the stress should be a lot less
roelof
roelofOP14mo ago
could be, we see which part the developing is
ἔρως
ἔρως14mo ago
sleep that's where
clevermissfox
clevermissfox14mo ago
Oh my goodness, you should get “KPow Discord Helper of the Year”! This is a crazy long and complicated thread. You rock!
ἔρως
ἔρως14mo ago
thank you, it was very tiring as it was a re-write from a heap into something that could pass a peer review it's not perfect, there's tiny tiny things that can be improved (like some capitalizations and the name of the plugin and stuff), but it's just nitpicky stuff for an exercise, and teaching good habits, i think we fulfulled the objective
roelof
roelofOP14mo ago
if I could I would give him 1000 or more points
ἔρως
ἔρως14mo ago
its fine, dont worry about it
roelof
roelofOP14mo ago
I do not worry
ἔρως
ἔρως14mo ago
did you submit it already?
roelof
roelofOP14mo ago
yep I send the url of the repo to the author of the challenge Do you want to change anything then?
ἔρως
ἔρως14mo ago
well, if you can sneak 2 edits
roelof
roelofOP14mo ago
always
ἔρως
ἔρως14mo ago
in the submit.php file, all your errors have the first parameter as "Error"
roelof
roelofOP14mo ago
no idea when he or she is looking
ἔρως
ἔρως14mo ago
that is a very terrible code
roelof
roelofOP14mo ago
oke, is there then a better statuscode like 200 for example I know 200 is wrong
ἔρως
ἔρως14mo ago
the purpose of that argument is to share a short code something like "validation" or "email-send" its something both humans and js code can understand
roelof
roelofOP14mo ago
oke, I have to think about which code then I find validation not a very good one
ἔρως
ἔρως14mo ago
i just told you 🤣 why not?
roelof
roelofOP14mo ago
of the two validation is then I think the best for my it is a error because there was something wrong
ἔρως
ἔρως14mo ago
i know, but you are already something that says that its an error you're saying that there was an error with the code "Error" just imagine that you want to handle the email error in a different way than the validation errors for example, imagine that the email error blocks the form permanently (dont do that)
roelof
roelofOP14mo ago
oke, I think I need to change my css then also
ἔρως
ἔρως14mo ago
no, you dont
roelof
roelofOP14mo ago
you are right
ἔρως
ἔρως14mo ago
i know i am
roelof
roelofOP14mo ago
Old problem that in such a "big" problem I loose what does what
ἔρως
ἔρως14mo ago
you're throwing away the value, in the forEach you just grab the .message and shove it into an array
roelof
roelofOP14mo ago
all changed
ἔρως
ἔρως14mo ago
can you send the link here? discord search is garbage on mobile
roelof
roelofOP14mo ago
of the repo ?
ἔρως
ἔρως14mo ago
yes
roelof
roelofOP14mo ago
moment, gitlab is acting wierd with a access code
ἔρως
ἔρως14mo ago
take your time
roelof
roelofOP14mo ago
I have to as lomg as I do not have a verification I cannot log in
ἔρως
ἔρως14mo ago
oh, that stuff
roelof
roelofOP14mo ago
yep the annoying 2MFA stuff get in with the help of google
roelof
roelofOP14mo ago
also done
ἔρως
ἔρως14mo ago
and now, you just need to change the name of the plugin
roelof
roelofOP14mo ago
that needs a lot of changes because all the domanins needs to be changed but I was thinking about chancing the name maybe rw_form ?
ἔρως
ἔρως14mo ago
no, the domain is fine you can change it to that but its fine if you dont im just talking about the plugin name, so we dont go into another rabbit hole
roelof
roelofOP14mo ago
I have no problem with chancing the name I think the custom_form is often used but I cannot think of a better name then rw_form or rw_shortcode_form any better suggestions ? Thinking if everything is approved to look if I can put the plugin dir to github Gitlab makes me crazy with the verification mails
ἔρως
ἔρως14mo ago
the 2nd one is finr fine but the first one is fine too
roelof
roelofOP14mo ago
Then I go for the second one it is more clear that a shortcode is used for the form also changed and pushed
ἔρως
ἔρως14mo ago
thats fine by me as long as the plugin name isnt the stupid tutorial you followed 🤦‍♂️
roelof
roelofOP14mo ago
🙂
ἔρως
ἔρως14mo ago
using a div as a form ... urgh
roelof
roelofOP14mo ago
that is the downside of following tutorials as a beginner you need them to learn something but you never know how good or bad the author is
ἔρως
ἔρως14mo ago
yes, but sometimes you land in gold, and other times you go face-first into a pile of manure i know the pain which is why i avoid tutorials and just get my hands dirty
roelof
roelofOP14mo ago
or if the tutorial is still valid with all the fast changes to gutenberg for example
ἔρως
ἔρως14mo ago
usually, it is
roelof
roelofOP14mo ago
I would but sometimes I have no clue how to begin and/or structure code
ἔρως
ἔρως14mo ago
i read documentation on it
roelof
roelofOP14mo ago
maybe wise to also learn the old php way of making templates these days or better stick to FSE and gutenberg ?
ἔρως
ἔρως14mo ago
you kinda did: everything was shortcodes and template parts until guttenberg was added and now wordpress uses blocks instead
roelof
roelofOP14mo ago
I mean for front end my "mentor" says only learn gutenberg and im making a template with it it is a struggle but I learn I can image that a lot of companies still wanted the old way and do not want to convert to gutenberg
ἔρως
ἔρως14mo ago
you just learned the "old" way its still used, as its a lot easier to inplement and doesnt require react in any way
roelof
roelofOP14mo ago
oke,, just one things less to learn but I m sure I have to keep on practizing so I could make this one also with just PHP : https://wpchallenges.beehiiv.com/p/wp-challenge-1-block-development I made that one with react but I wonder if It works when I do multiples ones in one page
ἔρως
ἔρως14mo ago
that site is hard to look at
ἔρως
ἔρως14mo ago
No description
roelof
roelofOP14mo ago
that is the wordpress site of the author of the challenges
ἔρως
ἔρως14mo ago
my eyes feel like they have needles inside 😦 it hurts to look at
roelof
roelofOP14mo ago
here is a screenshot
roelof
roelofOP14mo ago
No description
roelof
roelofOP14mo ago
I find this a reall y really ugly site https://www.speelotheekdewissel.nl/
ἔρως
ἔρως14mo ago
i have to tilt my phone to look at it
roelof
roelofOP14mo ago
it is a volunteer organisation that runs a toy library my dream is to make a better looking site with maybe a possiblity to keep track of members, toys and which members has lend which toy but it will take a very very long time to be at a level I can make such a site
ἔρως
ἔρως14mo ago
well, if you're messing with people's stuff, it is a big responsability
roelof
roelofOP14mo ago
What do you think of this site : https://visie-groep.nl/ That is the company that helps me to get my life in a good direction
Visie Groep
Ontwikkel jezelf bij Visie Groep - Visie Groep
Ontwikkel jezelf bij Visie Groep. Dit kan zowel op persoonlijk vlak zijn als werk-gerelateerd of een combinatie daarvan.
roelof
roelofOP14mo ago
I like it time for lunch . In 30 min a proffesional is here to help me so not online till 14.30 (Amsterdam time)
ἔρως
ἔρως14mo ago
it makes me hungry, thinking about peanut butter and jam
roelof
roelofOP14mo ago
the site of my remarks ?
ἔρως
ἔρως14mo ago
the site but i like it
roelof
roelofOP14mo ago
how do you make you hungry ? It is about helping people
ἔρως
ἔρως14mo ago
i know, but i cant read anything because of the language barrier so, i noticed the colors and "oh, this looks like purple jam"
roelof
roelofOP14mo ago
oke sorry , the site is not in English no idea of that can be implement easiliy but they work in a small area of the Netherlands so no real use for other languages and im not the person that is administrator
ἔρως
ἔρως14mo ago
its fine lol did you change the plugin name?
roelof
roelofOP14mo ago
yep and if good , it i is pushed
ἔρως
ἔρως14mo ago
looks good to me did you fix this?
roelof
roelofOP14mo ago
on local it is fixed and no sign that I did not save things and git status gave that everything is up to date very wierd sorry, one change is not pushed and I cannpt push it because of the stupid verification mail
ἔρως
ἔρως14mo ago
that is a pain im sure there's a better config for your repo
roelof
roelofOP14mo ago
Yep i think move it to github One repo for all plugins or a separate for everry plugin And delete all my html css and js repos I do not get any of the mails I miss then the history But thuis is not working Or move to bitbucket
ἔρως
ἔρως14mo ago
github has history also, there are ways to setup gitlab to never request your stuff, but that's for #os-and-tools
roelof
roelofOP14mo ago
GitHub
GitHub - RoelofWobben/rw_shortcode_form
Contribute to RoelofWobben/rw_shortcode_form development by creating an account on GitHub.
roelof
roelofOP14mo ago
and now with the change you asked for
ἔρως
ἔρως14mo ago
nice!
roelof
roelofOP14mo ago
github has a importer for gitlab 🙂
ἔρως
ἔρως14mo ago
interesting i actually though about having my own gitlab instance, but holy balls this thing is annoying but anyways, i will check your changes later
roelof
roelofOP14mo ago
now the first project and I can work from github 🙂 is oke
roelof
roelofOP14mo ago
and my first react project is moved : https://github.com/RoelofWobben/rw_testimonialCard
GitHub
GitHub - RoelofWobben/rw_testimonialCard
Contribute to RoelofWobben/rw_testimonialCard development by creating an account on GitHub.
ἔρως
ἔρως14mo ago
i will check in a bit
roelof
roelofOP14mo ago
as I said many times , take your time
ἔρως
ἔρως14mo ago
i sm sm am ... phone keyboarfs suck kryboards ...
roelof
roelofOP14mo ago
LOL The react you do not have to do something with it
ἔρως
ἔρως14mo ago
i will check it out anyways
roelof
roelofOP14mo ago
oke your life, your choices I know you do not like teaching and you do not like react
ἔρως
ἔρως14mo ago
its not a matter of liking or not react its a matter of being easy to make code thats unreadable to me react is amazing at a lot of stuff, but dont think that websites is one of those things
roelof
roelofOP14mo ago
This is not a website but a plugin/component that you can use to make a nice page but i agree for me react is the worst readable code from the js frameworks that I know
ἔρως
ἔρως14mo ago
so, website stuff and when you use tsx, then it's just vomit inducing
roelof
roelofOP14mo ago
and then do tailwind in the mix 😛 I think im old-skool. I like my css. js ,html in seperate places or files
ἔρως
ἔρως14mo ago
i like it separated too
roelof
roelofOP14mo ago
and I hope I can be a good full stack wordpress developer in the future
ἔρως
ἔρως14mo ago
practice and you will
roelof
roelofOP14mo ago
yep, that is why I do the wp-challenges to get practice a lot for myself I find it difficult to make up a challenge for msyself
ἔρως
ἔρως14mo ago
you're doing the right thing
roelof
roelofOP14mo ago
Happy to hear was the only site I could find that has "real" wp problems
ἔρως
ἔρως14mo ago
it seems like a good site
roelof
roelofOP14mo ago
we see the only thing that bothers me , that he does not look at the quality just checks if the code works not my style and you never know when you get a new challenge he says first week of the month
ἔρως
ἔρως14mo ago
should be soon then
roelof
roelofOP14mo ago
yep first the result from the challenge we made together and then a few days later a new one I find it very good of a person to make up such challenges and then every month for a junior, medior and senior I cannot do that
ἔρως
ἔρως14mo ago
i can't do that either but, lets wait for the challenge response
roelof
roelofOP14mo ago
yep im doing it right now Cannot do more at the moment
ἔρως
ἔρως14mo ago
just rest then
roelof
roelofOP14mo ago
yep, going to watch tv or you have remarks on one of my projects
ἔρως
ἔρως14mo ago
no, so far, not really
roelof
roelofOP14mo ago
🙂 If I have time I think im going to test if the Card works when there are more then 1 card but as you said first rest. Had a busy day
ἔρως
ἔρως14mo ago
yes, first rest
roelof
roelofOP14mo ago
rest a lot
ἔρως
ἔρως14mo ago
the only thing i would say that you should have done was to let the user pick an image
roelof
roelofOP14mo ago
that was the part that what not in the challenge and that is why I did not do it
ἔρως
ἔρως14mo ago
it's kinda the most important part, but i get it
roelof
roelofOP14mo ago
This what is what is given :
Task:

Develop a custom Gutenberg block that displays a testimonial card with fields for a testimonial quote, author name, and author's job title.

Requirements:

Create a custom block using @wordpress/create-block,

Implement a block that includes fields for testimonial quotes, author name, and job title,

Fields can be displayed either inside the block in the editor or on the side panel

Use Editor and Block Styles (editor if fields are displayed inside the block in the editor). Block Styles for the front.

Task:

Develop a custom Gutenberg block that displays a testimonial card with fields for a testimonial quote, author name, and author's job title.

Requirements:

Create a custom block using @wordpress/create-block,

Implement a block that includes fields for testimonial quotes, author name, and job title,

Fields can be displayed either inside the block in the editor or on the side panel

Use Editor and Block Styles (editor if fields are displayed inside the block in the editor). Block Styles for the front.

and if I test it on 2024 theme I would not work in a group and I did not test it when there are more then 1 card Will that do somewhere this week Now time to sleep no idea if it's difficult to change it so that the user can change the image
ἔρως
ἔρως14mo ago
i don't know either
roelof
roelofOP14mo ago
O< I thought you knew everything 🙂 Did some testing on the card Two cards work but as soon as I put a Card in a group in the 2024 theme , I cannot change any text No idea why that happens
roelof
roelofOP14mo ago
and if I may believe this tutorial a image upload is not so difficult https://awhitepixel.com/wordpress-gutenberg-add-image-select-custom-block/
AWhitePixel
A White Pixel
How to Add an Image Select in Your Custom WordPress Gutenberg Block...
This tutorial explains how to add an image select or upload button for the Media Library inside Inspector for your custom WordPress Gutenberg block.
ἔρως
ἔρως14mo ago
so much bs just to pick an image 😦
roelof
roelofOP14mo ago
yep maybe there is a better way but then I have to google more Checked the group problem also on 2023 but it stays Both on 2024 and 2023 I cannot change the text if I put the card in a group
ἔρως
ἔρως14mo ago
i wouldnt worry about it
roelof
roelofOP14mo ago
oke makes me very curious why and did I do something wrong
ἔρως
ἔρως14mo ago
you didnt do anything wrong
roelof
roelofOP14mo ago
Oke still wonder what then causs this because in the real world this plugin is not usable
ἔρως
ἔρως14mo ago
its fine, its an exercise
roelof
roelofOP14mo ago
I know but for me things have to be working andI know this is not working but I let it go because I have no clue why No error messages or whatever
ἔρως
ἔρως14mo ago
yeah, just rest ignore it, let it go
roelof
roelofOP14mo ago
I have to so no trying if Ican make a image upload so a user can change the image ? 😢
ἔρως
ἔρως14mo ago
do you want to do something cheeky? you can use a block content tag that only allows adding images
roelof
roelofOP14mo ago
you mean a shortcode that we did in the last 2 weeks then im using react and php together
ἔρως
ἔρως14mo ago
no no
roelof
roelofOP14mo ago
or a react component that allows that Would be I think a lot difficult
ἔρως
ἔρως14mo ago
<InnerBlocks/> you can set the allowedBlocks to only allow images
roelof
roelofOP14mo ago
Never worked with that one I know for a acccordion challenge I saw some one using it So for me then first find out what it does and how it works and have to find out how to save the images then
ἔρως
ἔρως14mo ago
what it does: allows adding blocks inside your block this takes a json array with the blocks you wanna add
roelof
roelofOP14mo ago
oke For me it would be a image upload block but is innerblock for beginners like me ???
ἔρως
ἔρως14mo ago
its for everybody
roelof
roelofOP14mo ago
looks more a senior or medior task
ἔρως
ἔρως14mo ago
looks harder than it is
roelof
roelofOP14mo ago
oke but what is then the advantage of using innerblocks with imageupload then direct use image upload ?
ἔρως
ἔρως14mo ago
if you want to allow other blocks, with an option of the user picking the layout as well, you can do it easily
roelof
roelofOP14mo ago
oke, so I could make it work that a user can choose a layout instead of hardcoding one like I did you made me curious So there goed the rest 🙂
ἔρως
ἔρως14mo ago
lol sorry for ruining your rest time but yes, thats the idea you can also allow links and paragraphs and studf
roelof
roelofOP14mo ago
NP I can even take care that the user can change the font-size, font-colors, background-color if I dream big
ἔρως
ἔρως14mo ago
thats way too far
roelof
roelofOP14mo ago
but nice to have and as far as I know not very diffucult to do is just adding this to the block.json
"supports": {
"color": {
"background": false,
"text": true
},
"html": false,
"typography": {
"fontSize": true
}
},
"supports": {
"color": {
"background": false,
"text": true
},
"html": false,
"typography": {
"fontSize": true
}
},
`
ἔρως
ἔρως14mo ago
nice
roelof
roelofOP14mo ago
if im doing this , I think I start over again with things
ἔρως
ἔρως14mo ago
try it
roelof
roelofOP14mo ago
so I know for certain that old code is not teasing me And the new challenge that can be there in any time ?
ἔρως
ἔρως14mo ago
when do you think it will come?
roelof
roelofOP14mo ago
Somewhere this week
ἔρως
ἔρως14mo ago
tomorrow or friday then
roelof
roelofOP14mo ago
last time I got the results and 2 days after it a new challenge But as I said earlier he says somewhere in the first week of a month so it could be even Saturday or Sunday and i have then till 31th to solve it But if its takes more time , also not a problem He says take all the time you need to solve a challenge innerblocks looks not so difficult as I read the page of it
ἔρως
ἔρως14mo ago
i told you, its very simple
roelof
roelofOP14mo ago
sorry
ἔρως
ἔρως14mo ago
but lets see what he says about your plugin
roelof
roelofOP14mo ago
but as I see it , you can make very dymanic blocks with it I still like your idea and then add the css part so the user can make a card as he/she likes
ἔρως
ἔρως14mo ago
you can do so much with it
roelof
roelofOP14mo ago
yep, I know That is also a pitfall because you can always add or improve things but at a certain point, you have to say it is good enough This one seems also be fun : it is the medior challenge of the block
Create a block that fetches and displays posts from a specific category dynamically.

Requirements:

Have a dropdown that shows categories on the side panel,

Display fetched posts within the block (It can be only a linked title or more info if you want).

Starting Tips:

Use ServerSideRender to render the posts,

Use apiFetch to get categories for displaying in the side panel,

Use the default REST API endpoints to fetch categories or create your own.
Create a block that fetches and displays posts from a specific category dynamically.

Requirements:

Have a dropdown that shows categories on the side panel,

Display fetched posts within the block (It can be only a linked title or more info if you want).

Starting Tips:

Use ServerSideRender to render the posts,

Use apiFetch to get categories for displaying in the side panel,

Use the default REST API endpoints to fetch categories or create your own.
but I think I have to learn a lot more to do that one for today Rest and watch tv GN
ἔρως
ἔρως14mo ago
that sounds a lot easier goodnight
roelof
roelofOP14mo ago
for me it sounds difficult I have to learn SSR, apiFetch and how to make REST API endpoints
ἔρως
ἔρως14mo ago
everything is way easier than you think
roelof
roelofOP14mo ago
We will see I had in mind to do first the junoir one to get more confident and then try the medior one And in the far far future even try to senior ones
ἔρως
ἔρως14mo ago
you will get there, dont worry
roelof
roelofOP14mo ago
im not worry When I do not get there it is also good but I can always dream big the senior one is to make a accordion I have a nice one with html so maybe later this year I can build it with wordpress I will try to take small steps and see where it brings me this is the medior of the form challenge
Extend the form from Task 1 to create a customizable contact form so fields can be defined through code & all entries are saved before being sent.

Requirements:

Have a function that will return fields for the contact form

Render fields from the function

Add actions before and after rendering fields

Create a custom post type “Contact Entries”

Before sending entries, create a custom post type for it

Change columns of the custom post type for contact entries
Extend the form from Task 1 to create a customizable contact form so fields can be defined through code & all entries are saved before being sent.

Requirements:

Have a function that will return fields for the contact form

Render fields from the function

Add actions before and after rendering fields

Create a custom post type “Contact Entries”

Before sending entries, create a custom post type for it

Change columns of the custom post type for contact entries
Then I have to look how to make a custom post type and a custom post type and this is senior one :
Create a custom table for saving entries instead of CPT and a 3rd party service for sending emails.

Requirements:

Create a custom table with columns: subject, message, email & custom (TEXT type and saving other data as serialized JSON)

Admin page to display data from the custom table + delete button to delete the row

Use SendGrid or another service of your choice to send emails (filter out wp_mail with SendGrid smtp details OR use the SendGrid API to send email directly)
Create a custom table for saving entries instead of CPT and a 3rd party service for sending emails.

Requirements:

Create a custom table with columns: subject, message, email & custom (TEXT type and saving other data as serialized JSON)

Admin page to display data from the custom table + delete button to delete the row

Use SendGrid or another service of your choice to send emails (filter out wp_mail with SendGrid smtp details OR use the SendGrid API to send email directly)
so still a lot to learn but that is oke maybe somewhere in the future
ἔρως
ἔρως14mo ago
this is basic stuff
roelof
roelofOP14mo ago
oops So I still now know basic stuff So a very lot to learn but first the innerblocks challenge or the challenge that is send hopefully soon
ἔρως
ἔρως14mo ago
yes, do it first
roelof
roelofOP14mo ago
and then I can look if there is time to do another one the medior one of block challenge looks also a good one to try and now really GN
ἔρως
ἔρως14mo ago
goodnight, and rest
roelof
roelofOP14mo ago
Thanks GM if I hear nothing this week, what is a nice one to tackle ?
ἔρως
ἔρως14mo ago
thats a good question
roelof
roelofOP14mo ago
I could do the card but then with innerblocks, the second form one or the second card one
ἔρως
ἔρως14mo ago
which one seems more interesting?
roelof
roelofOP14mo ago
Hsve to think about it. Can learn from all three
ἔρως
ἔρως14mo ago
its best to wait for the next challenge
roelof
roelofOP14mo ago
Oke
ἔρως
ἔρως14mo ago
and this way, you rest and spend time with your daughter
roelof
roelofOP14mo ago
That is correct
ἔρως
ἔρως14mo ago
did you got any feedback?
roelof
roelofOP14mo ago
nope totally no emails frustatingf
ἔρως
ἔρως14mo ago
its fine
roelof
roelofOP14mo ago
??
ἔρως
ἔρως14mo ago
its normal to take a few days to give feedback
roelof
roelofOP14mo ago
oke we see still waiting on feedback of the Card one and that i submit some 2 - 3 weeks ago I can say good bye but he is the only one to have practice in wordpress
ἔρως
ἔρως14mo ago
probably is overwhelmed
roelof
roelofOP14mo ago
but as I said we have still some more to choose Can be, I think some 4 - 6 persons work on his challenge
ἔρως
ἔρως14mo ago
and i doubt its just 4-6 answering it
roelof
roelofOP14mo ago
????
ἔρως
ἔρως14mo ago
im sure they have more than 4-6 submissions for each exercise
roelof
roelofOP14mo ago
No idea
ἔρως
ἔρως14mo ago
i have no idea too
roelof
roelofOP14mo ago
Found back a email of january where he stated this:
For now, I got some questions and examples from 2-3 more. That’s all :P
For now, I got some questions and examples from 2-3 more. That’s all :P
so it seems that not much people work on these challenges
ἔρως
ἔρως14mo ago
yeah, that isnt surprising
roelof
roelofOP14mo ago
is it ?
ἔρως
ἔρως14mo ago
i mean, where will you find a full team that just produces wordpress challenges?
roelof
roelofOP14mo ago
No where Hé is only one and i think it is a one man shop maybe im the only one to solve that challenge and he waits for more
ἔρως
ἔρως14mo ago
that doesnt make sense, as doing 1 is faster than many at the same time
roelof
roelofOP14mo ago
yep I know there is one who had done all three of the form challenges
ἔρως
ἔρως14mo ago
that is crazy but probably experienced
roelof
roelofOP14mo ago
Totally no idea I only know his name Igor Benić yep a xp one
roelof
roelofOP14mo ago
WordPress
Igor Benic (@ibenic) - WordPress user profile
WordPress Developer who loves to develop themes and plugins. In my free time I am working on personal projects built upon WordPress.
roelof
roelofOP14mo ago
Ever heard of him ??
ἔρως
ἔρως14mo ago
nope, i didnt
roelof
roelofOP14mo ago
I also not So wait and wait I hate to be waiting and be depend on someone GN Tomorrow work further on the template and in the afternoon another proffesional to help me to get better mental so not much coding even if it was needed
ἔρως
ἔρως14mo ago
goodnight i know it sucks, but you can't do much
roelof
roelofOP14mo ago
I know and I hate it in 30 min leave for working at the template
ἔρως
ἔρως14mo ago
what? o.O
roelof
roelofOP14mo ago
What do you not understatement?
ἔρως
ἔρως14mo ago
this
roelof
roelofOP14mo ago
oke, i mean that in 30 min I have to leave to goto work and "work" on my template challenge there
ἔρως
ἔρως14mo ago
and you managed to work on it?
roelof
roelofOP14mo ago
yep three divs readu I can put it on github also when you want to see it that will then be on Monday
ἔρως
ἔρως14mo ago
you can put it when youre free
roelof
roelofOP14mo ago
No the code so far is on a place where i casnot work at home
ἔρως
ἔρως14mo ago
oh, alright
roelof
roelofOP14mo ago
so Monday I can put it on github
ἔρως
ἔρως14mo ago
put it up when you can
roelof
roelofOP14mo ago
oke Did you ever made a template with just Gutenvberg and a child-theme ? or are you more the php way ? and maybe choose anothrt challenge It looks like there will no new one this month maybe this one :
Medior Challenge: Fetch & Display Dynamic Content

Points: 2

Task:

Create a block that fetches and displays posts from a specific category dynamically.

Requirements:

Have a dropdown that shows categories on the side panel,

Display fetched posts within the block (It can be only a linked title or more info if you want).

Starting Tips:

Use ServerSideRender to render the posts,

Use apiFetch to get categories for displaying in the side panel,

Use the default REST API endpoints to fetch categories or create your own.
Medior Challenge: Fetch & Display Dynamic Content

Points: 2

Task:

Create a block that fetches and displays posts from a specific category dynamically.

Requirements:

Have a dropdown that shows categories on the side panel,

Display fetched posts within the block (It can be only a linked title or more info if you want).

Starting Tips:

Use ServerSideRender to render the posts,

Use apiFetch to get categories for displaying in the side panel,

Use the default REST API endpoints to fetch categories or create your own.
I think I need some categories and posts ? With a standard install of wordpress there are not much of both
ἔρως
ἔρως14mo ago
just php for me, im not too big on react yup, wordpress doesnt have any posts by default, just 1 page
roelof
roelofOP14mo ago
oke, if I do that , I have to see how then tests this plugin
roelof
roelofOP14mo ago
maybe this one to make fake posts : https://nl.wordpress.org/plugins/fakerpress/
Gustavo Bordoni
Plugin Directory
FakerPress
FakerPress is een schone manier om nep- en dummy-inhoud te genereren voor je WordPress, ideaal voor ontwikkelaars die tests nodig hebben
ἔρως
ἔρως14mo ago
you can just write them yourself
roelof
roelofOP14mo ago
could no idea how many categories and posts per category I then need
ἔρως
ἔρως14mo ago
2-3 is fine
roelof
roelofOP14mo ago
Oké so 4 posts
ἔρως
ἔρως14mo ago
yup, should be enough
roelof
roelofOP14mo ago
Oke First sleep and tomorrow or monday i Will see what i do
ἔρως
ἔρως14mo ago
oh, yeah, that's a good idea
roelof
roelofOP14mo ago
But this one looks not so complex Gn
ἔρως
ἔρως14mo ago
goodnight
roelof
roelofOP14mo ago
Do not know if that challenge is for me I have googled a lot but do not see how I can fetch the posts of a catergory with SSR I can use the API as I did with the categories but that is not SSR Or I can use the WP_query
ἔρως
ἔρως14mo ago
if you use php, to get you the categories, just use the WP_Queryclass
roelof
roelofOP14mo ago
oke but the requirements says this:
Use the default REST API endpoints to fetch categories or create your own.
Use the default REST API endpoints to fetch categories or create your own.
and I believe because this is a gutenberg component, I have to use react
roelof
roelofOP14mo ago
Gutenberg Hub
How to create dynamic server-side WordPress Gutenberg block - Guten...
In this tutorial, we’re going to create a dynamic Gutenberg server-side block. Pre-requisites As always, Here are some recommended pre-requisites […]
roelof
roelofOP14mo ago
still no news about new challenges 😢 angry at my choice to make it in react ??
ἔρως
ἔρως14mo ago
lets hope he says something soon no, it doesn't make a difference to me yeah, you will have to use the api then luckly, the api is very easy to use
roelof
roelofOP14mo ago
This night or tomorrow I have to find out why the code gives no respons on the backend
export default function Edit() {

const [categories, setCategories] = useState("");

apiFetch( { path: '/wp-json/v2/categories' } ).then( ( categories ) => {
setCategories(categories);
} );

return (
<p { ...useBlockProps() }>
{ __(
categories,
'rw-filter-categories'
) }
</p>
);
}
export default function Edit() {

const [categories, setCategories] = useState("");

apiFetch( { path: '/wp-json/v2/categories' } ).then( ( categories ) => {
setCategories(categories);
} );

return (
<p { ...useBlockProps() }>
{ __(
categories,
'rw-filter-categories'
) }
</p>
);
}
It still looks like categories is empty @ἔρως any hints here ?
ἔρως
ἔρως14mo ago
dont you have to await it, to get an answer from apiFetch?
ἔρως
ἔρως14mo ago
try it
roelof
roelofOP14mo ago
before the set Categories or before the categories in the html?
ἔρως
ἔρως14mo ago
you know what? now, i don't know this is react, and i don't know react
roelof
roelofOP14mo ago
no problem but I see a interesting error message :
Uncaught (in promise)
Object { code: "rest_no_route", message: "Geen route gevonden die overeenkomt met de URL en aanvraagmethode.", data: {…} }

code: "rest_no_route"

data: Object { status: 404 }

message: "Geen route gevonden die overeenkomt met de URL en aanvraagmethode."
Uncaught (in promise)
Object { code: "rest_no_route", message: "Geen route gevonden die overeenkomt met de URL en aanvraagmethode.", data: {…} }

code: "rest_no_route"

data: Object { status: 404 }

message: "Geen route gevonden die overeenkomt met de URL en aanvraagmethode."
It seems there is no route for the url and the POST methos wierd because when I do :

http://learn.test/wp-json/wp/v2/categories

http://learn.test/wp-json/wp/v2/categories
in the browser I see a answer
ἔρως
ἔρως14mo ago
you probably miswrote it
roelof
roelofOP14mo ago
I will try to solve this tomorrow When I try this :

await apiFetch('http://learn.test/wp-json/wp/v2/categories').then( ( categories ) => {
setCategories(categories);
} );

await apiFetch('http://learn.test/wp-json/wp/v2/categories').then( ( categories ) => {
setCategories(categories);
} );
I see error mesage that the response is not a valid json respons
ἔρως
ἔρως14mo ago
you're probably hitting cors issues or something i have no idea
roelof
roelofOP14mo ago
And this seems to be valid json to me :

[{"id":1,"count":2,"description":"","link":"http:\/\/learn.test\/category\/niet-gecategoriseerd\/","name":"Niet gecategoriseerd","slug":"niet-gecategoriseerd","taxonomy":"category","parent":0,"meta":[],"_links":{"self":[{"href":"http:\/\/learn.test\/wp-json\/wp\/v2\/categories\/1"}],"collection":[{"href":"http:\/\/learn.test\/wp-json\/wp\/v2\/categories"}],"about":[{"href":"http:\/\/learn.test\/wp-json\/wp\/v2\/taxonomies\/category"}],"wp:post_type":[{"href":"http:\/\/learn.test\/wp-json\/wp\/v2\/posts?categories=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}]

[{"id":1,"count":2,"description":"","link":"http:\/\/learn.test\/category\/niet-gecategoriseerd\/","name":"Niet gecategoriseerd","slug":"niet-gecategoriseerd","taxonomy":"category","parent":0,"meta":[],"_links":{"self":[{"href":"http:\/\/learn.test\/wp-json\/wp\/v2\/categories\/1"}],"collection":[{"href":"http:\/\/learn.test\/wp-json\/wp\/v2\/categories"}],"about":[{"href":"http:\/\/learn.test\/wp-json\/wp\/v2\/taxonomies\/category"}],"wp:post_type":[{"href":"http:\/\/learn.test\/wp-json\/wp\/v2\/posts?categories=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}]
Time to sleep maybe tomorrow I will understand this
ἔρως
ἔρως14mo ago
it does look valid to me too
roelof
roelofOP14mo ago
so why do I get the message that is is not valid
But now really time to sleep and read more about apiFetch in wordpress GN
ἔρως
ἔρως14mo ago
goodnight
roelof
roelofOP14mo ago
@ἔρως did you ever see a message that the json is not valid
ἔρως
ἔρως14mo ago
yes, usually when html is sent with the json
roelof
roelofOP14mo ago
oke Here I do not have a clue why this error message is shown
/**
* Retrieves the translation of text.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';

/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { useBlockProps } from '@wordpress/block-editor';
import { useState } from '@wordpress/element';
import apiFetch from '@wordpress/api-fetch';

/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* Those files can contain any CSS code that gets applied to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './editor.scss';

/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
*
* @return {Element} Element to render.
*/
export default async function Edit() {

const [categories, setCategories] = useState("");

apiFetch( {path: '/wp-json/wp/v2/categories'}).then( ( categories ) => {
console.log(categories);
} );

return (
<p { ...useBlockProps() }>
{ __(

"test",
'rw-filter-categories'
) }
</p>
);
}

/**
* Retrieves the translation of text.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';

/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { useBlockProps } from '@wordpress/block-editor';
import { useState } from '@wordpress/element';
import apiFetch from '@wordpress/api-fetch';

/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* Those files can contain any CSS code that gets applied to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './editor.scss';

/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
*
* @return {Element} Element to render.
*/
export default async function Edit() {

const [categories, setCategories] = useState("");

apiFetch( {path: '/wp-json/wp/v2/categories'}).then( ( categories ) => {
console.log(categories);
} );

return (
<p { ...useBlockProps() }>
{ __(

"test",
'rw-filter-categories'
) }
</p>
);
}

or why the route is not found With some help I solved it this wa
useEffect(() => {
async function fetchCategories() {
const response = await fetch("/wp-json/wp/v2/categories");
if (!response.ok) {
return;
}

const categories = await response.json();
console.log(categories);
}

fetchCategories();
}, []);

useEffect(() => {
async function fetchCategories() {
const response = await fetch("/wp-json/wp/v2/categories");
if (!response.ok) {
return;
}

const categories = await response.json();
console.log(categories);
}

fetchCategories();
}, []);

ἔρως
ἔρως14mo ago
and that works?
roelof
roelofOP14mo ago
yep I see now a json respons Have to think now how to get the "name" field out of all and stick in a sort of state
ἔρως
ἔρως14mo ago
this is where my knowledge is very very limited
roelof
roelofOP14mo ago
mine also google helps sometimes
ἔρως
ἔρως14mo ago
thats good, because i cant help a lot with react
roelof
roelofOP14mo ago
not a problem I find it a pity that you do not want to learn to make plugins in react But your life , your choices
ἔρως
ἔρως14mo ago
its not that i work, then i want to relax and there are easier tools to do it anyway
roelof
roelofOP14mo ago
oke also well
ἔρως
ἔρως14mo ago
yeah, i sometimes only get to relax at 7pm, which is really awful
roelof
roelofOP14mo ago
ooo I much earlier I work now from 9 till 11 and hopefully soon from 9 till 1
ἔρως
ἔρως14mo ago
9am to 11pm?
roelof
roelofOP14mo ago
no 9am till 11 am so 2 hours and the wp challengesI do in my free time
ἔρως
ἔρως14mo ago
oh, thats good thrn then
roelof
roelofOP14mo ago
and I hope to get back till 9am till 1 pm
ἔρως
ἔρως14mo ago
and that pays more?
roelof
roelofOP14mo ago
it pays nothing Our counsil pays the company and I get benefits from the counsil of my city the purpose it to get me back to a payed job but then I have to make some big steps
ἔρως
ἔρως14mo ago
yeah, that sorta sounds like a good deal but yeah, just work your way up to the 4-5 hours
roelof
roelofOP14mo ago
it is my plan I was around 4 hours work but then the operation and corona hit me
ἔρως
ἔρως14mo ago
yeah, corona was devastating
roelof
roelofOP14mo ago
so again trying to come back But as I said earlier I have good help
ἔρως
ἔρως14mo ago
it's good to have some help how's it going?
roelof
roelofOP14mo ago
good
ἔρως
ἔρως14mo ago
i mean, the react thingy
roelof
roelofOP14mo ago
still busy im now trying to find out how I can take a name out of this :

Array [ {…} ]

0: Object { id: 1, count: 2, link: "http://learn.test/category/niet-gecategoriseerd/", … }
​​
_links: Object { self: (1) […], collection: (1) […], about: (1) […], … }
​​
count: 2
​​
description: ""
​​
id: 1
​​
link: "http://learn.test/category/niet-gecategoriseerd/"
​​
meta: Array []
​​
name: "Niet gecategoriseerd"
​​
parent: 0
​​
slug: "niet-gecategoriseerd"
​​
taxonomy: "category"
​​
<prototype>: Object { … }

Array [ {…} ]

0: Object { id: 1, count: 2, link: "http://learn.test/category/niet-gecategoriseerd/", … }
​​
_links: Object { self: (1) […], collection: (1) […], about: (1) […], … }
​​
count: 2
​​
description: ""
​​
id: 1
​​
link: "http://learn.test/category/niet-gecategoriseerd/"
​​
meta: Array []
​​
name: "Niet gecategoriseerd"
​​
parent: 0
​​
slug: "niet-gecategoriseerd"
​​
taxonomy: "category"
​​
<prototype>: Object { … }
this seems not to be working
const categories = await response.json();
categories = categories.map ( (category) => category.name)
const categories = await response.json();
categories = categories.map ( (category) => category.name)
ἔρως
ἔρως14mo ago
why are you awaiting a response?
roelof
roelofOP14mo ago
because I wait on a respons from the wordpress api here is the whole code :
useEffect(() => {
async function fetchCategories() {
const response = await fetch("/wp-json/wp/v2/categories");
if (!response.ok) {
return;
}

const categories = await response.json();
categories = categories.map ( (category) => category.name)
console.log(categories);
}

fetchCategories();
}, []);

useEffect(() => {
async function fetchCategories() {
const response = await fetch("/wp-json/wp/v2/categories");
if (!response.ok) {
return;
}

const categories = await response.json();
categories = categories.map ( (category) => category.name)
console.log(categories);
}

fetchCategories();
}, []);

I try to remember how I did the same in js
ἔρως
ἔρως14mo ago
https://developer.wordpress.org/block-editor/reference-guides/packages/packages-api-fetch/ <-- the example of the api is different from what you're doing but you're using fetch now?
roelof
roelofOP14mo ago
oops
ἔρως
ἔρως14mo ago
you need to pay attention to these things
roelof
roelofOP14mo ago
chips, and now that annoying invalid json again ;'(
ἔρως
ἔρως14mo ago
check what the server returns
roelof
roelofOP14mo ago
this :
Uncaught (in promise)
Object { code: "invalid_json", message: "De respons is not a valid json response." }

code: "invalid_json"
Uncaught (in promise)
Object { code: "invalid_json", message: "De respons is not a valid json response." }

code: "invalid_json"
ἔρως
ἔρως14mo ago
that's in a promise which means, that's not what the server returned
roelof
roelofOP14mo ago
and nothing to be found in the network tab
ἔρως
ἔρως14mo ago
check the filters
roelof
roelofOP14mo ago
I did
roelof
roelofOP14mo ago
the only thing I can find is this :
No description
roelof
roelofOP14mo ago
but that does not bring us closer to the solution
ἔρως
ἔρως14mo ago
that's a post request to ajax im sure that that wasn't your code your code is the ones that say fetch on the initiator column
roelof
roelofOP14mo ago
on all that ones I see no payload for this request Time to sleep here
ἔρως
ἔρως14mo ago
because it doesn't have a payload it's a GET request those don't have payloads
roelof
roelofOP14mo ago
it getting more wierd
ἔρως
ἔρως14mo ago
not really
roelof
roelofOP14mo ago
I see now this :
No description
roelof
roelofOP14mo ago
and that seems to be the answer
ἔρως
ἔρως14mo ago
that looks normal
roelof
roelofOP14mo ago
and I see now this error :
Objects are not valid as a React child (found: [missing argument]). If you meant to render a collection of children, use an array instead.
Objects are not valid as a React child (found: [missing argument]). If you meant to render a collection of children, use an array instead.
ἔρως
ἔρως14mo ago
but that returns an array
roelof
roelofOP14mo ago
yep and also no clue where it is but for now GN
ἔρως
ἔρως14mo ago
goodnight
roelof
roelofOP14mo ago
sorry that I was so rude but I was very tired after 2 nights bad sleep
ἔρως
ἔρως14mo ago
dude, you werent rude
roelof
roelofOP14mo ago
oke
ἔρως
ἔρως14mo ago
just relax
roelof
roelofOP14mo ago
Oke, I will do but I doubt if I go on with this challenge After 4 days still working on the apiFetch thing
ἔρως
ἔρως14mo ago
i think it stems from your misunderstanding on how a fetch works i notice some gaps in your knowledge in that area
roelof
roelofOP14mo ago
I think I know how fetch work but not with react maybe I have to find a good course on react and fetch
ἔρως
ἔρως14mo ago
the fetchApi and fetch work the exact same way both fetch something and return a promise
roelof
roelofOP14mo ago
oke
ἔρως
ἔρως14mo ago
you should take a break and learn a little about fetch and xhr
roelof
roelofOP14mo ago
oke
ἔρως
ἔρως14mo ago
im saying this so you understand how it usually works
roelof
roelofOP14mo ago
I know
ἔρως
ἔρως14mo ago
because i see you doing strange stuff like awaiting a json from a response
roelof
roelofOP14mo ago
you do not want to harsh me but want to understand what im doing
ἔρως
ἔρως14mo ago
no, i want you to understand what you are doing wrong, by making you learn the right way
roelof
roelofOP14mo ago
only bummer that the error message from react are vaque
ἔρως
ἔρως14mo ago
well, if you await a json inside of a non-async arrow function, all you get is a promise so, in the end, you're creating new promises from data you already fetched im sure that that will confuse you and anybody
roelof
roelofOP14mo ago
oke but the function is async
export default async function Edit() {

const [categories, setCategories] = useState("");

useEffect(() => {
async function fetchCategories() {
try {
const response = await apiFetch({
path: "/wp/v2/categories",
});
setCategories(categories) // response is already resolved to JSON
} catch (err) {
console.log(err);
}
}

fetchCategories();
}, []);
export default async function Edit() {

const [categories, setCategories] = useState("");

useEffect(() => {
async function fetchCategories() {
try {
const response = await apiFetch({
path: "/wp/v2/categories",
});
setCategories(categories) // response is already resolved to JSON
} catch (err) {
console.log(err);
}
}

fetchCategories();
}, []);
but still I get a message that I use the hooks the wrong way but if I google I see that every answer do it this way this seems to work :
/**
* Retrieves the translation of text.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';

/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { useBlockProps } from '@wordpress/block-editor';
import apiFetch from '@wordpress/api-fetch';
import React, { useState, useEffect } from "react";

/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* Those files can contain any CSS code that gets applied to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './editor.scss';




/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
*
* @return {Element} Element to render.
*/
export default function Edit() {

const [categories, setCategories] = useState("");

useEffect(() => {
async function fetchCategories() {
try {
const response = await apiFetch({
path: "/wp/v2/categories",
});
setCategories(categories) // response is already resolved to JSON
} catch (err) {
console.log(err);
}
}

fetchCategories();
}, []);

return (
<p { ...useBlockProps() }>
{ __(

categories,
'rw-filter-categories'
) }
</p>
);
}
/**
* Retrieves the translation of text.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';

/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { useBlockProps } from '@wordpress/block-editor';
import apiFetch from '@wordpress/api-fetch';
import React, { useState, useEffect } from "react";

/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* Those files can contain any CSS code that gets applied to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './editor.scss';




/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
*
* @return {Element} Element to render.
*/
export default function Edit() {

const [categories, setCategories] = useState("");

useEffect(() => {
async function fetchCategories() {
try {
const response = await apiFetch({
path: "/wp/v2/categories",
});
setCategories(categories) // response is already resolved to JSON
} catch (err) {
console.log(err);
}
}

fetchCategories();
}, []);

return (
<p { ...useBlockProps() }>
{ __(

categories,
'rw-filter-categories'
) }
</p>
);
}
the only thing is that categories is still empty Time to dive into xhr
ἔρως
ἔρως14mo ago
i would just brush through it, as fetch will be the more useful for the most part
roelof
roelofOP14mo ago
I know but the challenge says I have to use apiFetch but I see that the categories are read in but not displayed on the return part and I have to understand why got it working
/**
* Retrieves the translation of text.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';

/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { useBlockProps } from '@wordpress/block-editor';
import apiFetch from '@wordpress/api-fetch';
import React, { useState, useEffect } from "react";

/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* Those files can contain any CSS code that gets applied to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './editor.scss';




/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
*
* @return {Element} Element to render.
*/
export default function Edit() {

const [categories, setCategories] = useState("");

useEffect(() => {
async function fetchCategories() {
try {
const response = await apiFetch({
path: "/wp/v2/categories",
});
setCategories(categories) // response is already resolved to JSON
} catch (err) {
console.log(err);
}
}

fetchCategories();
}, []);

return (
<p { ...useBlockProps() }>
{ __(

categories,
'rw-filter-categories'
) }
</p>
);
}
/**
* Retrieves the translation of text.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';

/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { useBlockProps } from '@wordpress/block-editor';
import apiFetch from '@wordpress/api-fetch';
import React, { useState, useEffect } from "react";

/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* Those files can contain any CSS code that gets applied to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './editor.scss';




/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
*
* @return {Element} Element to render.
*/
export default function Edit() {

const [categories, setCategories] = useState("");

useEffect(() => {
async function fetchCategories() {
try {
const response = await apiFetch({
path: "/wp/v2/categories",
});
setCategories(categories) // response is already resolved to JSON
} catch (err) {
console.log(err);
}
}

fetchCategories();
}, []);

return (
<p { ...useBlockProps() }>
{ __(

categories,
'rw-filter-categories'
) }
</p>
);
}
gives now this :
Array [ "Niet gecategoriseerd" ]
Array [ "Niet gecategoriseerd" ]
Now I can try to put that into a drop down menu 🙂 problem for tomorrow
ἔρως
ἔρως14mo ago
i will read that a little better later on
roelof
roelofOP14mo ago
np this challenge is not running away
ἔρως
ἔρως14mo ago
i really can't understand what may be wrong
roelof
roelofOP14mo ago
in the new code nothing is wrong the only thing I did was to delete that Edit was also a async method
ἔρως
ἔρως14mo ago
so, it's working now?
roelof
roelofOP14mo ago
and I forget to assign categories yep this is working
/**
* Retrieves the translation of text.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';

/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { useBlockProps } from '@wordpress/block-editor';
import apiFetch from '@wordpress/api-fetch';
import React, { useState, useEffect } from "react";

/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* Those files can contain any CSS code that gets applied to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './editor.scss';




/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
*
* @return {Element} Element to render.
*/
export default function Edit() {

const [categories, setCategories] = useState("");

useEffect(() => {
async function fetchCategories() {
try {
const response = await apiFetch({
path: "/wp/v2/categories",
});
const category = response.map( (item) => item.name);
setCategories(category) // response is already resolved to JSON
} catch (err) {
console.log(err);
}
}

fetchCategories();
}, []);

return (
<p { ...useBlockProps() }>
{ __(

categories,
'rw-filter-categories'
) }
</p>
);
}
/**
* Retrieves the translation of text.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';

/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { useBlockProps } from '@wordpress/block-editor';
import apiFetch from '@wordpress/api-fetch';
import React, { useState, useEffect } from "react";

/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* Those files can contain any CSS code that gets applied to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './editor.scss';




/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
*
* @return {Element} Element to render.
*/
export default function Edit() {

const [categories, setCategories] = useState("");

useEffect(() => {
async function fetchCategories() {
try {
const response = await apiFetch({
path: "/wp/v2/categories",
});
const category = response.map( (item) => item.name);
setCategories(category) // response is already resolved to JSON
} catch (err) {
console.log(err);
}
}

fetchCategories();
}, []);

return (
<p { ...useBlockProps() }>
{ __(

categories,
'rw-filter-categories'
) }
</p>
);
}
ἔρως
ἔρως14mo ago
nice!
roelof
roelofOP14mo ago
now I get a array with categorie names tommorrow look how I can put these categories in a drop down choose a category put it also in a state and then I have to look how I can find all posts with that category and make a nice display and then I think ready 🙂
ἔρως
ἔρως14mo ago
that shouldn't be too hard
roelof
roelofOP14mo ago
no idea
ἔρως
ἔρως14mo ago
me neither
roelof
roelofOP14mo ago
Right now no idea how I can fetch all the posts with a category but google knows a way I think Right now Im thinking I need a wp-query but we see
ἔρως
ἔρως14mo ago
you can pass the category ids
roelof
roelofOP14mo ago
?? pass to what
ἔρως
ἔρως14mo ago
to the GET request for the posts
roelof
roelofOP14mo ago
oke so the same way as I did with api-fetch ?
ἔρως
ἔρως14mo ago
yup
roelof
roelofOP14mo ago
could work
ἔρως
ἔρως14mo ago
it should work
roelof
roelofOP14mo ago
I have to take some steps first to get there
ἔρως
ἔρως14mo ago
oh, yeah, you still have quite a bit to get there
roelof
roelofOP14mo ago
one step at the time and look if the api has a url for getting the posts of a category and I have to change my code Right now the category id is not saved in the state
ἔρως
ἔρως14mo ago
i have no idea how hard it will be to implement
roelof
roelofOP14mo ago
not hard I have to look again what the response is and change the set to a {} I think where thet key is the id and the value the name
ἔρως
ἔρως14mo ago
that's not hard then
roelof
roelofOP14mo ago
I have to look how to make this work
roelof
roelofOP14mo ago
I see now this error :
Objects are not valid as a React child (found: [missing argument]). If you meant to render a collection of children, use an array instead.
Objects are not valid as a React child (found: [missing argument]). If you meant to render a collection of children, use an array instead.
` I have think how to solve this I think I have to change the start value of the state but like i said I have to think to what oke options is this :
Array [ {…} ]

0: Object { label: "Niet gecategoriseerd", value: 1 }
Array [ {…} ]

0: Object { label: "Niet gecategoriseerd", value: 1 }
oke the problems was in the render so now I can use the id of the category 🙂 Time to sleep solution was to change the map to this ;
const options = response.map((category) => ({ label: category.name, value: category.id }));
const options = response.map((category) => ({ label: category.name, value: category.id }));
I go early to bed because tomorrow I get a MRI of my heart and have to be early at the hospital and yes there is a endpoint
http://localhost:3000/project/wp-json/wp/v2/posts?categories=16,17
http://localhost:3000/project/wp-json/wp/v2/posts?categories=16,17
ἔρως
ἔρως14mo ago
nice! you're basically reducing the amount of data that you pass around, which is nice you really need to sleep
roelof
roelofOP14mo ago
yes, boss and I see when I can and will work on this
ἔρως
ἔρως14mo ago
i don't understand 80% of the code you show, so, i can't help a ton
roelof
roelofOP14mo ago
np I understand it maybe I can make it clear with some jsdocs ? of course after the hospital or explain to you what the code does ?
ἔρως
ἔρως14mo ago
i look at it and understand most of it but the expectations of what it should do is what gets mr me
roelof
roelofOP14mo ago
It should ask the api for all categories , take the name and id out of the response and store it in a variable categories Edit.js takes care of the server side and render.js is the client side
roelof
roelofOP14mo ago
WordPress Developer Resources
WordPress Developer Resources
Tutorial: Build your first block – Block Editor Handbook | Develope...
In this tutorial, you will build a "Copyright Date Block"—a basic yet practical block that displays the copyright symbol (©), the current…
ἔρως
ἔρως14mo ago
is it working?
roelof
roelofOP14mo ago
My code yes Did not change anything today
ἔρως
ἔρως14mo ago
so, its done?
roelof
roelofOP14mo ago
No No Like I said I have done nothing I only give a respons to what you said that you do not understand 80% of my code no developing today. Had a MRI and a very very bad night so I rest a lot today
ἔρως
ἔρως14mo ago
yeah, i wont ask anything about this, so you dont get distracted with any codigo
roelof
roelofOP14mo ago
np the mri was at 8,30 so for me the past now now wait 10 days for the results 😦
ἔρως
ἔρως14mo ago
still, you gotta go slow so you dont overwork yourself
roelof
roelofOP14mo ago
yep I know that is why I decided to do not coding today
ἔρως
ἔρως14mo ago
good
roelof
roelofOP14mo ago
thanks still learning to keep my own boundaries
ἔρως
ἔρως14mo ago
having boundaries is very important to be a healthy developer
roelof
roelofOP14mo ago
i know, even if I not get a job as as developer, it is healty for every humna
ἔρως
ἔρως14mo ago
it is, and thats why im taking a break today as well
roelof
roelofOP14mo ago
💯 and I do not think I see a new challenge of wp-challenges this month he seems to be gone after mid February time for a late dinner
ἔρως
ἔρως14mo ago
that's actually sad the being gone, not the dinner
roelof
roelofOP14mo ago
we see if its really so, it is very sad it;s te only site I know with "real" wordpress challenges But I see he posts still on Twitter But no respons if you send him a email for a long time
ἔρως
ἔρως14mo ago
maybe he's just overwhelmed
roelof
roelofOP14mo ago
could be as lomg as he writes nothing nobody knows but I have still 2 to go 🙂
ἔρως
ἔρως14mo ago
lets see how it goes
roelof
roelofOP14mo ago
nothing more then I can do
ἔρως
ἔρως14mo ago
yup, besides having your dinner
roelof
roelofOP14mo ago
that is also the past we had chips so time to watch tv and sleep early I hope and tomorrow I try to make the drop down for the categories and store also that
ἔρως
ἔρως14mo ago
yup, save it for tomorrow
roelof
roelofOP14mo ago
I see how I feel tommorrow
ἔρως
ἔρως14mo ago
if you don't feel like it, just relax
roelof
roelofOP14mo ago
exactly GN
ἔρως
ἔρως14mo ago
goodnight
roelof
roelofOP14mo ago
selectbox is working Now time to find out how wp - api can help me figure out which posts are in what category and I like to use the per-page option so back to the manuals
ἔρως
ἔρως14mo ago
you can just set the categories parameter with the ids you need by the way, use the _fields parameter to reduce the amount of stuff you get from the api
roelof
roelofOP14mo ago
??? this is the right url: http://learn.test/wp-json/wp/v2/posts?categories=1 and if it;s possible I want also include the per_page parameter or do you mean :
const response = await apiFetch({
path: "/wp/v2/posts",
per_page : pagenumber
category: chosenCategory
});
const response = await apiFetch({
path: "/wp/v2/posts",
per_page : pagenumber
category: chosenCategory
});
ἔρως
ἔρως14mo ago
i dont think that that's right but i cant check now
roelof
roelofOP14mo ago
😢
ἔρως
ἔρως14mo ago
wordpress has a few query parameters you can set, and one of them is _fields when you search, wordpress has to render all the content for you, but you only need a few fields with that, you can say the fields you need and reduce the memory usage and increase performance
roelof
roelofOP14mo ago
oke I have to google and look how I can make the per-page and category dymamic and how the url must look like or can I do :
const response = await apiFetch({
path: '/wp/v2/posts?{chosenCategory}&per_page={pagenumber}`
});
const response = await apiFetch({
path: '/wp/v2/posts?{chosenCategory}&per_page={pagenumber}`
});
then I have to only find where that _fields` part must be placed
ἔρως
ἔρως14mo ago
you're doing it wrong
roelof
roelofOP14mo ago
oke, i looked at that one but do not see how to deal with 2 things The category and the number of posts a page
ἔρως
ἔρως14mo ago
those are query arguments
roelof
roelofOP14mo ago
oke, I see it now
ἔρως
ἔρως14mo ago
the api is pretty weird, i know
roelof
roelofOP14mo ago
so I can do :
mport apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';

const queryParams = { category: chosenCategory, per_page: page-number };

apiFetch( { path: addQueryArgs( '/wp/v2/posts', queryParams ) } ).then( ( posts ) => {
console.log( posts );
} );
mport apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';

const queryParams = { category: chosenCategory, per_page: page-number };

apiFetch( { path: addQueryArgs( '/wp/v2/posts', queryParams ) } ).then( ( posts ) => {
console.log( posts );
} );
ἔρως
ἔρως14mo ago
basically, that's it
roelof
roelofOP14mo ago
Changed it to my situation pressed enter to soon
ἔρως
ἔρως14mo ago
page-number <-- this wont work
roelof
roelofOP14mo ago
😢 better use then pagenumber ? and we have that _fields thing I have to figure out
ἔρως
ἔρως14mo ago
do you want to subtract number from page? easy check what the browser returns when you visit the api, in a new tab firefox helps a lot here, btw
roelof
roelofOP14mo ago
I see a lot
ἔρως
ἔρως14mo ago
now, pick the fields you need
roelof
roelofOP14mo ago
No description
ἔρως
ἔρως14mo ago
for example, do you need the _links?
roelof
roelofOP14mo ago
nope all the things beginning with _ I do not need
ἔρως
ἔρως14mo ago
you probably just need the id and the title
roelof
roelofOP14mo ago
not the id Im thinking of date, title, author, content
ἔρως
ἔρως14mo ago
well, you want to select a post, right?
roelof
roelofOP14mo ago
is not in the assignment
ἔρως
ἔρως14mo ago
so, what do you need to do with the posts then?
roelof
roelofOP14mo ago
it is only stated to display the posts , not a link to another page
ἔρως
ἔρως14mo ago
oh, then yeah, that should be enough
roelof
roelofOP14mo ago
this is stated :
Display fetched posts within the block (It can be only a linked title or more info if you want).
Display fetched posts within the block (It can be only a linked title or more info if you want).
ἔρως
ἔρως14mo ago
that's perfect so, the _field can just be an array with this
roelof
roelofOP14mo ago
so maybe I can use the id to make the title a link
ἔρως
ἔρως14mo ago
if you want you don't need it
roelof
roelofOP14mo ago
but that link never would work 😦
ἔρως
ἔρως14mo ago
you have the link property
roelof
roelofOP14mo ago
so rather make a nice maybe even a sort of blog posts template but you were talking about the _fields thing
ἔρως
ἔρως14mo ago
yes, to limit how much junk comes from the api
roelof
roelofOP14mo ago
I can then use that for the categories thing There is also a big junk that I do not need
ἔρως
ἔρως14mo ago
yup, it reduces the api response time as well, and makes your plugin feel more responsive
roelof
roelofOP14mo ago
oke , So I can do :
const queryParams = { category: chosenCategory, per_page: page-number, _fields=date, title, author, content };
const queryParams = { category: chosenCategory, per_page: page-number, _fields=date, title, author, content };
ἔρως
ἔρως14mo ago
no, it's an array of strings you still didn't fix the page minus number
roelof
roelofOP14mo ago
oke
const queryParams = { category: chosenCategory, per_page: pagenumber, _fields=[date, title, author, content] };
const queryParams = { category: chosenCategory, per_page: pagenumber, _fields=[date, title, author, content] };
ἔρως
ἔρως14mo ago
that's an array of Uncaught exception: undefined variable date strings have quotes
roelof
roelofOP14mo ago
const queryParams = { category: chosenCategory, per_page: pagenumber, _fields=['date', 'title', 'author', 'content'] };
const queryParams = { category: chosenCategory, per_page: pagenumber, _fields=['date', 'title', 'author', 'content'] };
`
ἔρως
ἔρως14mo ago
perfect wait, no you have _fields= instead of _fields:
roelof
roelofOP14mo ago
oke but I can do the same with thr category part now I get the date I wanted and a lot of junk WAUW if this work , then I only have to write some html can css to display the posts and then ready
ἔρως
ἔρως14mo ago
yup, that should be it
roelof
roelofOP14mo ago
a new record for me A plugin in a few days instead of a few weeks
ἔρως
ἔρως14mo ago
the other one was a big re-write of something that was very very badly written this one, you're doing most/all from scratch that's the difference
roelof
roelofOP14mo ago
Then if there no new challenge I can even try this one :
ask:

Extend the form from Task 1 to create a customizable contact form so fields can be defined through code & all entries are saved before being sent.

Requirements:

Have a function that will return fields for the contact form

Render fields from the function

Add actions before and after rendering fields

Create a custom post type “Contact Entries”

Before sending entries, create a custom post type for it

Change columns of the custom post type for contact entries

Starting Tips:

Use apply_filters when returning fields from the function

When saving entries, Subject = post title, message = post content, email = custom meta

All other fields are also saved as custom meta

Creating custom post type

Customizing Admin Columns
ask:

Extend the form from Task 1 to create a customizable contact form so fields can be defined through code & all entries are saved before being sent.

Requirements:

Have a function that will return fields for the contact form

Render fields from the function

Add actions before and after rendering fields

Create a custom post type “Contact Entries”

Before sending entries, create a custom post type for it

Change columns of the custom post type for contact entries

Starting Tips:

Use apply_filters when returning fields from the function

When saving entries, Subject = post title, message = post content, email = custom meta

All other fields are also saved as custom meta

Creating custom post type

Customizing Admin Columns
that will be a new PHP challenge So more difficult for me 😦
ἔρως
ἔρως14mo ago
so, basically, that's to implement contact form 7
roelof
roelofOP14mo ago
no idea I do not know all plugins
ἔρως
ἔρως14mo ago
it's a contact form plugin that does exactly what's in the challenge
roelof
roelofOP14mo ago
LOL , so he looks at existing plugins and make a challenge
ἔρως
ἔρως14mo ago
in this case, he might have though about it by himself
roelof
roelofOP14mo ago
But I can try after I did the react one and a break after solving it
ἔρως
ἔρως14mo ago
a break is always a good idea
roelof
roelofOP14mo ago
or somewhere in the future but first solve this one
ἔρως
ἔρως14mo ago
yes, it shouldn't take too long
roelof
roelofOP14mo ago
no idea I have to think of a nice layout and im not a designer 😢 but you are right , I solved almost all the challenge 🙂 maybe there is a future for me as a wp developer but still enought to learn and practice, practice and practice
ἔρως
ἔρως14mo ago
practice is the most important if i were you, i would just add a centered h1 with the title, the date under and then the excerpt (not the content, as the content can be massive and have lots of plugins)
roelof
roelofOP14mo ago
maybe I will
ἔρως
ἔρως14mo ago
you can then add a read more link, but it should be automatic
roelof
roelofOP14mo ago
oke, then I can use the except instead of the content
ἔρως
ἔρως14mo ago
yup which is a lot shorter also, if the post has the same block in it, i don't know if it will create an infinite loop, so, excerpt wins
roelof
roelofOP14mo ago
oke so if I have to post I can make a loop with some div to display it ?
ἔρως
ἔρως14mo ago
what do you mean?
roelof
roelofOP14mo ago
I have asked for all the posts of a category that will return a div so I thought I could some sort of loop to display all the posts
ἔρως
ἔρως14mo ago
oh, yeah, you have to
roelof
roelofOP14mo ago
so some work to to the next few days and i have to look how I can make it work the the posts are not "loaded" before a category is chosen
ἔρως
ἔρως14mo ago
what do you mean?
roelof
roelofOP14mo ago
When a user has not chosen a category then in my opoion there must be no posts shown
ἔρως
ἔρως14mo ago
yes, you can show something saying "select a category"
roelof
roelofOP14mo ago
for example I will see how I can make that work
ἔρως
ἔρως14mo ago
you should be able to, it doesn't sound hard
roelof
roelofOP14mo ago
we see when I get there maybe it is small problem, maybe not
ἔρως
ἔρως14mo ago
yeah, we see when you get there
roelof
roelofOP14mo ago
GN tomorrow morning working on the template for 2 hours Hopefully I get the first page ready
ἔρως
ἔρως14mo ago
im sure you will be able to do it goodnight
roelof
roelofOP14mo ago
GN one remark
roelof
roelofOP14mo ago
I was thinking of this layout for the posts https://wordpress.com/theme/ron?tab_filter=blog
WordPress.com
roelof
roelofOP14mo ago
without the image
ἔρως
ἔρως14mo ago
try it, and see how it goes
roelof
roelofOP14mo ago
that is my plan
ἔρως
ἔρως14mo ago
go for it, it's a good plan
roelof
roelofOP14mo ago
thanks for the confidence in me
ἔρως
ἔρως14mo ago
you're welcome, im just trying to get you to keep going
roelof
roelofOP14mo ago
I m still going with sometimes my own ideas
I like to make good code and I like it when things look well
ἔρως
ἔρως14mo ago
as long as you format your code properly and don't cram everything into a single line, it will be fine oh, and proper documentation and indentation
roelof
roelofOP14mo ago
oke, that I also have to change this one does not have jsdoc
ἔρως
ἔρως14mo ago
you should get into the habit of using it unless you use typescript then you can just use typescript's stuff
roelof
roelofOP14mo ago
no idea if wordpress understands TS I have played with TS
ἔρως
ἔρως14mo ago
no, but the compiler does but don't know if react supports it
roelof
roelofOP14mo ago
I think so
ἔρως
ἔρως14mo ago
im not sure if it does, but its fine
roelof
roelofOP14mo ago
oke, this seems to work :
useEffect(() => {
async function fetchCategories() {
try {
const queryParams = { _fields: ['name', 'id'] };
const response = await apiFetch({
path: addQueryArgs("/wp/v2/categories", queryParams),
});
const options = response.map((category) => ({ label: category.name, value: category.id }));
setCategories(options) // response is already resolved to JSON
} catch (err) {
console.log(err);
}
}

fetchCategories();
}, []);
useEffect(() => {
async function fetchCategories() {
try {
const queryParams = { _fields: ['name', 'id'] };
const response = await apiFetch({
path: addQueryArgs("/wp/v2/categories", queryParams),
});
const options = response.map((category) => ({ label: category.name, value: category.id }));
setCategories(options) // response is already resolved to JSON
} catch (err) {
console.log(err);
}
}

fetchCategories();
}, []);
now lunch and sleep a lot
ἔρως
ἔρως14mo ago
that looks good to me
roelof
roelofOP14mo ago
🙂
roelof
roelofOP14mo ago
ἔρως
ἔρως14mo ago
i will check in a bit
roelof
roelofOP14mo ago
and I have a problem
roelof
roelofOP14mo ago
I do this : but now the compiler thinks Category is a string where I think it must be a array of a string and a int
roelof
roelofOP14mo ago
Have to think how to solve that one
ἔρως
ἔρως14mo ago
on the phone, discord just shows an attachment
No description
ἔρως
ἔρως14mo ago
so, i will check latwr later
roelof
roelofOP14mo ago
no problem the prpblem is on the onChange part
ἔρως
ἔρως14mo ago
i will see what i find in about 3 hours
roelof
roelofOP14mo ago
take all the time you need as I said many times before
ἔρως
ἔρως14mo ago
i know i just wish i was napping but napping is what got me here: working late
roelof
roelofOP14mo ago
then do it I slept the whole afternoon because of a busy week working late is never good 😢 Code can wait
ἔρως
ἔρως14mo ago
yeah, but i have legal obligations to fulfill 😭
roelof
roelofOP14mo ago
Bummer I hope for you that it will not take much time
ἔρως
ἔρως14mo ago
i finally got free and used my accrued overtime
roelof
roelofOP14mo ago
pfffff i almost go to bed and you are just ready of work crazy
ἔρως
ἔρως14mo ago
it's my fault anyways why do you have this:
const queryParams = { category: chosenCategory, per_page: 1, _fields:['date', 'title', 'author', 'content'] };
const response = await apiFetch({
path: addQueryArgs("/wp/v2/posts", queryParams),
});
const options = response.map((post) => ({ date: post.date, title: post.title, author: post.author, content: post.content }));
setPosts(options)
const queryParams = { category: chosenCategory, per_page: 1, _fields:['date', 'title', 'author', 'content'] };
const response = await apiFetch({
path: addQueryArgs("/wp/v2/posts", queryParams),
});
const options = response.map((post) => ({ date: post.date, title: post.title, author: post.author, content: post.content }));
setPosts(options)
instead of just this:
const queryParams = { category: chosenCategory, per_page: 1, _fields:['date', 'title', 'author', 'content'] };
const response = await apiFetch({
path: addQueryArgs("/wp/v2/posts", queryParams),
});
setPosts(response);
const queryParams = { category: chosenCategory, per_page: 1, _fields:['date', 'title', 'author', 'content'] };
const response = await apiFetch({
path: addQueryArgs("/wp/v2/posts", queryParams),
});
setPosts(response);
roelof
roelofOP14mo ago
did not know if that worked I will change that tomorrow
ἔρως
ἔρως14mo ago
the api returns an array, and you defined which fields you're receiving from the api how do i know it returns an array? because you're running .map on it and you're just re-creating the object you receive save yourself the work
roelof
roelofOP14mo ago
oke not smart
ἔρως
ἔρως14mo ago
and if you don't know if it works, just try the worst that can happen is ... not working
roelof
roelofOP14mo ago
yep, and both ways I learn something 🙂
ἔρως
ἔρως14mo ago
yup, but you save yourself a ton of work and you save battery as well
roelof
roelofOP14mo ago
my own battery ? That one is totally empty The computer battery is not a problem I use a desktop computer 🙂
ἔρως
ἔρως14mo ago
on a mobile phone, battery is a premium
roelof
roelofOP14mo ago
🙂
ἔρως
ἔρως14mo ago
if you can shortcut stuff and save some work, consider it
roelof
roelofOP14mo ago
Im old skool. I use my mobile for calling and for seeing how I must ride with my car So then I use google maps Still wonder why on line 85 the onChange Category is a strimg Hopefully I see it tomorrow when my brain is more relaxed
ἔρως
ἔρως14mo ago
onChange A function that receives the value of the new option that is being selected as input. If multiple is true the value received is an array of the selected value. If multiple is false the value received is a single value with the new selected value. Type: function Required: Yes
notice this detail:
[...] that receives the value of the new option [...]
that's why it's a string https://developer.wordpress.org/block-editor/reference-guides/components/select-control/
roelof
roelofOP14mo ago
bummer BEcause of the apiFetch I need there the id of the category and with your changes the categorie drop down is empty 😢 he, response look well but when I do :
SetCategories(response)
SetCategories(response)
categories stays empty
roelof
roelofOP14mo ago
see this screenshot :
No description
roelof
roelofOP14mo ago
first is the response and second is the categories
ἔρως
ἔρως14mo ago
so weird 🤔
roelof
roelofOP14mo ago
yep, I think we are hit by the wierd reactivity of react but I agree puttningg back the map is also wierd because we have the data already in the right form
ἔρως
ἔρως14mo ago
yeah it's so strange
roelof
roelofOP14mo ago
js frameworks are strange in this sort of things but just ike you do not have any clue how to proceed I have a idea we have this :
const response = await apiFetch({
path: addQueryArgs("/wp/v2/categories", queryParams),
});
const response = await apiFetch({
path: addQueryArgs("/wp/v2/categories", queryParams),
});
but the examples on the page looks like this :
apiFetch( { path: addQueryArgs( '/wp/v2/posts', queryParams ) } ).then( ( posts ) => {
console.log( posts );
} );
apiFetch( { path: addQueryArgs( '/wp/v2/posts', queryParams ) } ).then( ( posts ) => {
console.log( posts );
} );
` so I think I have to change some code This is a little bit better :
const queryParams = { _fields: ['name', 'id'] };
var data = {}
await apiFetch({
path: addQueryArgs("/wp/v2/categories", queryParams),
}).then ((response) => {data = response});
console.log(data);
setCategories(data)
console.log(data)
const queryParams = { _fields: ['name', 'id'] };
var data = {}
await apiFetch({
path: addQueryArgs("/wp/v2/categories", queryParams),
}).then ((response) => {data = response});
console.log(data);
setCategories(data)
console.log(data)
but the drop-down keeps to be empty but the two console log's are not both the sam
ἔρως
ἔρως14mo ago
why are you using .then when you await?
roelof
roelofOP14mo ago
you are right await is removed
ἔρως
ἔρως14mo ago
that won't work now but you should be able to move the setCategories inside the then
roelof
roelofOP14mo ago
yep, I saw it Everything is empty now chips, when I do this :
apiFetch({
path: addQueryArgs("/wp/v2/categories", queryParams),
}).then ((response) => {setCategories(response)});
} catch (err) {
console.log(err);
}
apiFetch({
path: addQueryArgs("/wp/v2/categories", queryParams),
}).then ((response) => {setCategories(response)});
} catch (err) {
console.log(err);
}
the drop down is still empty this is not funny anymore
ἔρως
ἔρως14mo ago
why do you have a catch?
roelof
roelofOP14mo ago
because it is needed by the try otherwise you get a error and when I try this :
useEffect(() => {
async function fetchCategories() {
try {
const queryParams = { _fields: ['name', 'id'] };
apiFetch({ path: addQueryArgs('/wp/v2/categories', queryParams) }).then((response) => {
setCategories(response);
});
} catch (err) {
console.log(err);
}
}
fetchCategories();
}, []);
useEffect(() => {
async function fetchCategories() {
try {
const queryParams = { _fields: ['name', 'id'] };
apiFetch({ path: addQueryArgs('/wp/v2/categories', queryParams) }).then((response) => {
setCategories(response);
});
} catch (err) {
console.log(err);
}
}
fetchCategories();
}, []);
I do not see the drop down at all and also no errors whatever very annoying
roelof
roelofOP14mo ago
sorry, im confused now What do you want exactly I do not think I can mix a react promise with a js promise
ἔρως
ἔρως14mo ago
there's no such thing as a "react promise" https://react.dev/reference/react/use <-- this just links to the mdn article
roelof
roelofOP14mo ago
still very confused
ἔρως
ἔρως14mo ago
how? it's very straightforward it's just a promise there's nothing special to it
roelof
roelofOP14mo ago
I do not see what exaclty to change at my code
ἔρως
ἔρως14mo ago
dont use a try catch
roelof
roelofOP14mo ago
oke I do not see how to make it work with the page you were reffering to
ἔρως
ἔρως14mo ago
check the examples
roelof
roelofOP14mo ago
I did
ἔρως
ἔρως14mo ago
then extrapolate the example to your situation
roelof
roelofOP14mo ago
and that part I do not see dinner time
ἔρως
ἔρως14mo ago
yeah, take a break you will realize it's way easier than what you're thinking
roelof
roelofOP14mo ago
oke I did this:
apiFetch({ path: addQueryArgs('/wp/v2/categories', queryParams) }).then((response) => {
setCategories(response).catch((error) => {console.log(error)});
});
apiFetch({ path: addQueryArgs('/wp/v2/categories', queryParams) }).then((response) => {
setCategories(response).catch((error) => {console.log(error)});
});
and see now a message that setCategories is undefined which makes no sense because I defined it a few lines before it
const [categories, setCategories] = useState([]);
const [categories, setCategories] = useState([]);
ἔρως
ἔρως14mo ago
i think it's best if you ask in #front-end because i really have no idea
roelof
roelofOP14mo ago
I also not
ἔρως
ἔρως14mo ago
also, you're misusing the .catch but it's fine
roelof
roelofOP14mo ago
I almost at the point I throw the towel
ἔρως
ἔρως14mo ago
finding errors is fine it happens
roelof
roelofOP14mo ago
I know but fighting against errors that does not make sense or no errors at all is very frustating
ἔρως
ἔρως14mo ago
it is
roelof
roelofOP14mo ago
Make a topic
roelof
roelofOP14mo ago
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
roelof
roelofOP14mo ago
hopefully someone give a respons
ἔρως
ἔρως14mo ago
yeah, hopefully
roelof
roelofOP14mo ago
wordpress can sometimes be a pain I almost have my first page ready but m y margins get overwritten by a faulty margin And I have to find out Monday how to solve that I see more and more that Wordpress is fine except when you want to do sometimes that is already defined in a parent theme then it is a pain in the ass to make things work but all we can do it wait
ἔρως
ἔρως14mo ago
nah, this is just react/js misunderstandings and i don't know how to help you because i dont use react
roelof
roelofOP14mo ago
totally not a problem
ἔρως
ἔρως14mo ago
thank you
roelof
roelofOP14mo ago
yw I see that you really try to help me maybe I could have better tried the form one where you said that form 7 must be re-created
ἔρως
ἔρως14mo ago
no, no you're learning react it's a very popular ... thing
roelof
roelofOP14mo ago
yep some love it and some hate it but I think wordpress does not learn you standard react looks like to me that theyt took a small portion out of it and changed some things
ἔρως
ἔρως14mo ago
it "requires" you to stick to a specific way of doing things, so it works the way you intend
roelof
roelofOP14mo ago
time to watch tv and parcitate in a quiz 🙂
ἔρως
ἔρως14mo ago
yes, just relax
roelof
roelofOP14mo ago
yep, cannot do more at the moment
ἔρως
ἔρως14mo ago
it's not just that it's just so you don't get too frustrated
roelof
roelofOP14mo ago
im already that I hate that I cannot solve this
ἔρως
ἔρως14mo ago
i dont have enough knowledge to help you on it
roelof
roelofOP14mo ago
I know and that is not a problem
ἔρως
ἔρως14mo ago
that's good
roelof
roelofOP14mo ago
I asked also on reddit . My code seems to be good But the drop down stays empty 😢
ἔρως
ἔρως14mo ago
are you getting any data?
roelof
roelofOP14mo ago
it seems I do
ἔρως
ἔρως14mo ago
then check if you do
roelof
roelofOP14mo ago
when I do console.log(response) I see the categories
ἔρως
ἔρως14mo ago
where do you put it?
roelof
roelofOP14mo ago
between the call and the setCategories line
ἔρως
ἔρως14mo ago
then check inside whatever renders the categories
roelof
roelofOP14mo ago
I will first a lot of sleep
ἔρως
ἔρως14mo ago
yes, it's late for you
roelof
roelofOP14mo ago
not for you ?? it is here almost 10 o clock so not very late
ἔρως
ἔρως14mo ago
almost 9pm, which isn't late at all
roelof
roelofOP14mo ago
i agree
ἔρως
ἔρως14mo ago
i usually sleep at 5-8am on weekends
roelof
roelofOP14mo ago
that is very short
ἔρως
ἔρως14mo ago
oh, no, i start sleeping at that time i usually sleep until 1-6pm
roelof
roelofOP14mo ago
oke I slept last evening almost 12 hours From 11 pm till 10 am Is your family happy with the strange sleeping pattern ? if I may ask
ἔρως
ἔρως14mo ago
i live with friends
roelof
roelofOP14mo ago
oke I have a family with a wife and a daugther
ἔρως
ἔρως14mo ago
if i had one, i wouldn't have the same sleeping pattern
roelof
roelofOP14mo ago
who knows what the future will bring
ἔρως
ἔρως14mo ago
yup, who knows?
roelof
roelofOP14mo ago
only time if you said before 2000 that in the next 5 years I would find my wife, getting married, get a child and move to another part of my country I would say you are crazy and it all happened
ἔρως
ἔρως14mo ago
stuff can change really quickly
roelof
roelofOP14mo ago
yep to the good or to the bad but GN and I hope that tomorrow someone has a hint why things are not working
ἔρως
ἔρως14mo ago
lets see but don't stress over it goodnight
roelof
roelofOP14mo ago
Seems that I need the map after all After 2 days no other solutions found on this channel and reddit I will wait for more responses
ἔρως
ἔρως14mo ago
if the map works, use it
roelof
roelofOP14mo ago
I think I will but I agree with you , it is doing the same as we already have so very wierd we need it
ἔρως
ἔρως14mo ago
very very weird indeed i don't know why the behaviour, but it's react
roelof
roelofOP14mo ago
yep and on stackoverflow I see the same sort of solution
ἔρως
ἔρως14mo ago
that is even weirder
roelof
roelofOP14mo ago
I have asked even there to see if there is another solution https://wordpress.stackexchange.com/questions/424202/why-is-my-drop-down-empty
WordPress Development Stack Exchange
Why is my drop down empty
For a wordpress challenge I need to make a dropdown with all categories and I need to use apifetch So I have this : /** * Retrieves the translation of text. * * @see https://developer.wordpress....
roelof
roelofOP14mo ago
for now GN tomorrow hopefully my first blcok template page ready
ἔρως
ἔρως14mo ago
hopefully someone answers goodnight yeah, nobody is going to answer it not to be mean or anything, but, the question there needs more details
roelof
roelofOP14mo ago
bummer which details do it need more ?
ἔρως
ἔρως14mo ago
how's it being used in the block? what are you trying to do? do you have any errors? does the api send anything? currently, all you have is a code dump, which is nearly impossible to answer
roelof
roelofOP14mo ago
changed it and I hope it is now good enough to be answered GN
ἔρως
ἔρως14mo ago
hopefully it is enough goodnight
roelof
roelofOP14mo ago
we see and otherwise I will make the map back
ἔρως
ἔρως14mo ago
just add it back, if it works
roelof
roelofOP14mo ago
Another question : Im thinking to do the form one But little confused. in the simple one we did :
[my_form message="message]
[my_form message="message]
and then in the plugin :
function roelof_add_custom_shortcode()
{
function roelof_contact_form($atts, $content, $shortcode_tag)
{
static $counter = 0;
$counter++;
wp_enqueue_style('rw-custom-form-style');
wp_enqueue_script('rw-custom-form-script');
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode' => $shortcode_tag,
'prefix' => $shortcode_tag . $counter,
]);
return ob_get_clean();
}

add_shortcode('contact_form', 'roelof_contact_form');
}

add_action('init', 'roelof_add_custom_shortcode');
function roelof_add_custom_shortcode()
{
function roelof_contact_form($atts, $content, $shortcode_tag)
{
static $counter = 0;
$counter++;
wp_enqueue_style('rw-custom-form-style');
wp_enqueue_script('rw-custom-form-script');
ob_start();
load_template(__DIR__ . '/templates/form.php', false, [
'atts' => $atts,
'content' => $content,
'shortcode' => $shortcode_tag,
'prefix' => $shortcode_tag . $counter,
]);
return ob_get_clean();
}

add_shortcode('contact_form', 'roelof_contact_form');
}

add_action('init', 'roelof_add_custom_shortcode');
but if I read the hints I have to use now :
function my_contact_fields() {
$fields = [
'email' => [
'type' => 'email',
'name' => 'email',
'label' => __( 'Email', 'textdomain' ),
'validate' => 'function_to_validate'
]
];

return apply_filters( 'my_contact_fields, $fields );
}
function my_contact_fields() {
$fields = [
'email' => [
'type' => 'email',
'name' => 'email',
'label' => __( 'Email', 'textdomain' ),
'validate' => 'function_to_validate'
]
];

return apply_filters( 'my_contact_fields, $fields );
}
To define the fields Does that mean the shortcode will now be [my_form] ?
ἔρως
ἔρως14mo ago
no, thats completely different the stuff you add to the shortcode will be in the $args variable
roelof
roelofOP14mo ago
I saw that already and when I use the apply_filter I think I do not need the args one because I cannot define it on 2 places
ἔρως
ἔρως14mo ago
well, it's not that it's just that it doesn't exist outside the shortcode function
roelof
roelofOP14mo ago
still confused. When I do it on 2 places I can get two different things on forms and that is not good
ἔρως
ἔρως14mo ago
what do you mean?
roelof
roelofOP14mo ago
let's say I make a shortcode like this : `[form message= "message"] But on the apply_filters I do :
function my_contact_fields() {
$fields = [
'email' => [
'type' => 'email',
'name' => 'email',
'label' => __( 'Email', 'textdomain' ),
'validate' => 'function_to_validate'
]
];

return apply_filters( 'my_contact_fields, $fields );
}
function my_contact_fields() {
$fields = [
'email' => [
'type' => 'email',
'name' => 'email',
'label' => __( 'Email', 'textdomain' ),
'validate' => 'function_to_validate'
]
];

return apply_filters( 'my_contact_fields, $fields );
}
then I have a difference. So I think it can lead to wierd bugs
ἔρως
ἔρως14mo ago
those have nothing to do one with the other
roelof
roelofOP14mo ago
oke I try to see how I can begin this one :
Extend the form from Task 1 to create a customizable contact form so fields can be defined through code & all entries are saved before being sent.

Requirements:

Have a function that will return fields for the contact form

Render fields from the function

Add actions before and after rendering fields

Create a custom post type “Contact Entries”

Before sending entries, create a custom post type for it

Change columns of the custom post type for contact entries

Starting Tips:

Use apply_filters when returning fields from the function

When saving entries, Subject = post title, message = post content, email = custom meta

All other fields are also saved as custom meta

Creating custom post type

Customizing Admin Columns
Extend the form from Task 1 to create a customizable contact form so fields can be defined through code & all entries are saved before being sent.

Requirements:

Have a function that will return fields for the contact form

Render fields from the function

Add actions before and after rendering fields

Create a custom post type “Contact Entries”

Before sending entries, create a custom post type for it

Change columns of the custom post type for contact entries

Starting Tips:

Use apply_filters when returning fields from the function

When saving entries, Subject = post title, message = post content, email = custom meta

All other fields are also saved as custom meta

Creating custom post type

Customizing Admin Columns
but I think I stick on the junior ones and wait till a new one comes along I still miss a lot of practice or xp to deal with the medior ones 😦
ἔρως
ἔρως14mo ago
oh, thats completely different
roelof
roelofOP14mo ago
how so, this is a medior challenge of wp-challenges and I fail to see what to change to make my old form to a solution of this challenge
ἔρως
ἔρως14mo ago
the way that contact form 7 does this is a post with specific shortcodes you dont need to go that far
roelof
roelofOP14mo ago
I know but I try to see what the challenge exactly wants
ἔρως
ἔρως14mo ago
shortcodes can receive content
roelof
roelofOP14mo ago
because there is talking about extending the old form it looks to me I need a shortcode and the apply-filter
ἔρως
ἔρως14mo ago
you can make shortcodes just for inputs and stuff one for a textarea one for radio buttons one for checkbox then the shortcodes come inside the final shortcode
roelof
roelofOP14mo ago
oke, I have to let this sink in
ἔρως
ἔρως14mo ago
alright
roelof
roelofOP14mo ago
I think I pass . IM afraid to get into a big rabbit hole
ἔρως
ἔρως14mo ago
its alright, i get it
roelof
roelofOP14mo ago
im get more and more the feeling that plugin work is not for me. but as I said I will wait for another beginners one
ἔρως
ἔρως14mo ago
shortcodes are super annoying
roelof
roelofOP14mo ago
I know. I read that most of them could be replaced by react component(s) Wordpress can be easy but as soon as you want to dive deeper it can be very difficult for a beginner like me
ἔρως
ἔρως14mo ago
the worst is that most content online is only teaching the basics and there are some things where wordpress really falls on it's face
roelof
roelofOP14mo ago
yep and most worst is that some content is made by people who do bad stuff and/or have no idea what they are doing and im stuck between the basics and some advantage stuff and forms are a hell in many languages
roelof
roelofOP14mo ago
I do not know if this is a good tutorial : https://tommcfarlin.com/custom-filters-with-shortcodes/
Tom
Tom McFarlin
Using Custom Filters with Shortcodes | Tom McFarlin
Even if you’ve never used either the API or the functionality before, it’s easy to begin implementing custom filters with shortcodes.
roelof
roelofOP14mo ago
but if I understand it well the apply_filters can overwrite the parts I wrote on the shortcode
ἔρως
ἔρως14mo ago
i will check it after work
roelof
roelofOP14mo ago
Oke
ἔρως
ἔρως14mo ago
im sure there's an easier way
roelof
roelofOP14mo ago
I could not find any the last few days
ἔρως
ἔρως14mo ago
i will check in a bit
roelof
roelofOP14mo ago
oke totally no hurry
ἔρως
ἔρως14mo ago
i don't see how it can help
roelof
roelofOP14mo ago
NP
ἔρως
ἔρως14mo ago
i mean, it is neat that you can do what's in the blog, but that's it
roelof
roelofOP14mo ago
oke I was looking how I could make the challenge work and that could be a path
ἔρως
ἔρως14mo ago
try it maybe it will work
roelof
roelofOP14mo ago
oke

Did you find this page helpful?