Category Archives: Development

WordPress – Making a custom type not have a url

Custom post type should have below settings:

‘rewrite’ => false,
‘query_var’ => false,
‘publicly_queryable’ => false,
‘public’ => false

Explanation of each:

rewrite Triggers the handling of rewrites for this post type. To prevent rewrites, set to false.

query_var ‘false’ – Disables query_var key use. A post type cannot be loaded at /?{query_var}={single_post_slug}

publicly_queryable Whether queries can be performed on the front end as part of parse_request().

public ‘false’ – Post type is not intended to be used publicly and should generally be unavailable in wp-admin and on the front end unless explicitly planned for elsewhere.

Go to Permalinks Settings and without changing anything – save change. It will rebuild your “routing”.

Source: https://stackoverflow.com/questions/19747096/remove-url-structure-for-custom-post-type

Default WordPress htaccess to enforce SSL

To have a redirect to non www with https can do the following:

 

Notes for Udemy Course “The Complete React Web Developer Course (with Redux)”

Notes for Udemy Course “The Complete React Web Developer Course (with Redux)” – https://www.udemy.com/react-2nd-edition/

React uses jsx and ES6 which means it requires Babel ( requires node ). Two scripts are required to work with React in the browser. React and ReactDOM. Below is a sample React component that has  toggle visibility functionality.

 

If components are nested, data as props can only be sent down – not up. So, on the parent component a method can be passed to the child component as a prop in order to send the information back to the parent.

Stateless functional components

Another type of component  that can be reduced to a single function call because they do not have any state to handle of their own. Mostly used for “presentation” type components. They can use props but aren’t accessible through *this* .

This type of component is “faster” as it has less overhead as it doesn’t handle state.

Small tricks for conditional rendering

For the arrow function, if you are return just an object you can use the following syntax:

In React, methods to update a parent component are passed down to child components via props.

Lifecycle methods

Methods are available to hook in to when a class based component is added/removed/updated in React. Common ones to use are componentDidMount() for fetching initial data, and componentDidUpdate(prevProps, prevState) for saving updated changes back to the db.

When using componentDidUpdate, make sure the prevState and current state are not equal ( !==) so that a save to db/localStorage is not called that is not needed.

 

Localstorage Notes

When within a method that was attached to a onClick event ( or any other similar method ), you can reference the field/value by the following type of example:

Children props ( slots )

Dynamic content that be added between a components tag can be added to be placed within the layout of the components it’s self. For example:

 

Simple form value/onChange practice:

Can create a single handleChange method to handle all form values and updates that are used in state.

Now we can use same method for all form fields:

 

Misc React Notes

In JSX, all normal html attributes such as onclick and onchange MUST be camel case.. i.e.e onClick and onChange

All components must start with a capital letter.

JSX conditional rendering :

ternary operator is accepted i.e. true ? ‘show this if true’ : ‘ value if false’

if/else can not be used in {} jsx since it is a statement and not an expression.

&& AND Operator

 

Notes for using Ajax in WordPress

While in WordPress admin area..  the variable named ajaxurl is available for javascript scripts to use for the url for ajax. Every call must at the very least have an “action” data property passed to WordPress in order for it to know which function will be used on the back-end to complete the ajax request.

The associated php that would be needed to process above :

Any ajax request must be handles by a function which has to be added via add_action

Note that any action you specify in the ‘action’ field for the data sent to the server will have ‘wp_ajax_’ prepended to the actions name.

Also need to bind to the action prefix ‘wp_ajax_nopriv’ so that the ajax will still work for visitors who are not logged in.

So the action ‘method_that_handles_the_ajax_request’ will have to be added in php as ‘wp_ajax_method_that_handles_the_ajax_request’ as seen in the php snippet.

If the action can not be found by admin-ajax.php the response returned from the ajax request will be an integer of zero ( 0 ).

If you created an ajax action method properly and you keep receiving a 0 response…confirm that your ajax action method was added.

Any function in php that handles a ajax request MUST end with die();

Within the php function, any post variable that is passed via the ajax data field, in our case the ‘address’ or ‘action’ field, is available via $_POST[“address”] or $_POST[“action”]


wp_localize_script

Used to add data to javascript that is normally only available on server side. Has to be used after an enqueue of a script and use the same “handle”. It will make the variables passed via localize_script available to the rest of the javascript that was enqueued.

Typically, this is used for nuances

Example of adding the nuances

Another way to add it when there are times where an additional script isn’t needed could be done in the following way.

 

 

 

Now we can update the javascript to use the nonce to pass back to WordPress to check for nonce validity.

Now that the nonce is created and is being passed from javascript to WordPress.. we setup below in php in the action that the ajax requests  to process it’s integrity.

 

Good notes for ajax usage: https://developer.wordpress.org/plugins/javascript/enqueuing/

 check_ajax_referer( 'nonce' );  This function will check nonce and if it fails will automatically kill the action it was ran within

 wp_send_json() will send back appropriate header and json back to javascript as well as wp_die() for the action it was called in.

Python script to scrape array of urls and create an excel spreadsheet

I created this in order to have a bootstrap for wanting to create a script to generate an excel spreadsheet from scraped information from an array of urls. Comments within the script indicate which values/columns/headers need to be updated.