Skip to content

Instantly share code, notes, and snippets.

@khoand0000
khoand0000 / prevent-parsley-double-submitting.md
Created November 9, 2015 23:48
Prevent Parsley double submitting when using javascript to initializing

click submit form A, form is valid: form A trigger submit event (1st time) --> form:submit event will be triggered --> Parsley make form A submit again (2rd time).

window.Parsley.on('form:submit', function (instance) {
    instance.submitEvent.preventDefault();
    return false;
});
@khoand0000
khoand0000 / parsley.md
Last active November 9, 2015 23:44
Why Parsley permit submit even form is invalid, using Parsley by javascript?
  • HTML file
<form id="my-form">
<input type="email" name="email" required/>
<button type="submit">submit</button>
</form>
  • Javascript file
  • My purpose: customize message after save post (post_type=at_order).
  • Solution: using admin_notices, redirect_post_location, post_updated_messages.
  • How: after saving post with post_type=at_order, redirect to /wp-admin/post.php?post=123&action=edit&message=1 --> wordpress will show message base on message parameter.
  • Case 1: I want to customize message (post_type=at_order) when message=1, using post_updated_messages filter.
function postUpdatedMessages($messages)
{
    $messages['at_order'][1] = __('blabla', AloThau::$text_domain); // change

 return $messages;
window.Parsley.options.successClass = "has-success";
window.Parsley.options.errorClass = "has-error";
window.Parsley.options.errorsWrapper = "<p class='help-block'></p>";
window.Parsley.options.errorTemplate = "<span></span>";
window.Parsley.options.classHandler = function (el) {
return el.$element.closest(".form-group"); // is used to put successClass or errorClass
};
window.Parsley.options.errorsContainer = function (el) {
return el.$element.closest(".form-group"); // is used to put successClass or errorClass
};
@khoand0000
khoand0000 / validate-inputs.html
Last active October 28, 2015 10:55
validate inputs (number, email) by using pattern & show custom error messages. suggestion: using http://parsleyjs.org/
<!-- number is positive -->
<input type="number" pattern="[0-9]+" />
<!-- email -->
<input type="email" pattern="([a-z0-9_]+|[a-z0-9_]+\.[a-z0-9_]+)*@([a-z0-9]|[a-z0-9]+\.[a-z0-9]+)+\.([a-z]{2,4})" />
<!-- Vietnamese phone -->
<input type="tel" pattern="0[0-9]{9,10}" />
<!--
@khoand0000
khoand0000 / money-format.php
Created October 28, 2015 10:00
money_format is not working with Vietnamese
$number = 1234.56;
// let's print the international format for the en_US locale
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";
// USD 1,234.56
// but if using vi_VN or vi_VN.UTF-8, not working
setlocale(LC_MONETARY, 'vi_VN');
echo money_format('%i', $number) . "\n";
@khoand0000
khoand0000 / save-json.php
Created October 27, 2015 22:47
note when saving json with unicode by json_encode()
$content = [];
// ...
$my_post = array(
'post_title' => 'aaa',
'post_content' => str_replace("\\", '\\\\', json_encode($content)), // avoid saving \u1ec5 to u1ec5
'post_status' => 'pending',
'post_author' => 0,
);
// Insert the post into the database
@khoand0000
khoand0000 / disable-add-new-button.md
Last active October 22, 2015 08:26
disable "Add New" button for custom post type
  • My case: I have a custom post type is called custom_post and a new role is manager. I want manager role can edit and list custom_post but can't add new custom_post, manager don't have permission for post (don't see post).
  • I used the code when register custom_post type
'capabilities' => ["create_posts" => "create_custom_posts"]

and did't add create_custom_posts, edit_posts caps to manger role

  • Problem: user has manager role can edit custom_post by access /wp-admin/post.php?post=9873&action=edit url, but he/she can't list custom_post by access /wp-admin/edit.php?post_type=custom_post.
  • I found down the cause is that manager role must have edit_posts cap to access /wp-admin/edit.php?post_type=custom_post
  • Solutions:
  • add create_custom_posts cap to manager role. But it is not right for my purpose
@khoand0000
khoand0000 / get-all-meta.php
Created October 21, 2015 05:29
get all meta (single) of post has $id
$meta = array_map(function ($a) {
return $a[0];
}, get_post_meta($id));
@khoand0000
khoand0000 / loadCss.js
Created October 15, 2015 10:37
loadCss.js
function loadCss(url) {
var link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = url;
document.getElementsByTagName("head")[0].appendChild(link);
}