Interview Prepartion, Uncategorized

WordPress Notes

1. Enque css and js
- copy below code
***********************
add_action( 'wp_enqueue_scripts', 'add_frontend_css' );

function add_frontend_css() {
//css
wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/frontend/css/bootstrap.min.css', false, '1.1', 'all');
wp_enqueue_style( 'font', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css', false, '1.1', 'all');
wp_enqueue_style( 'tinyslider', get_template_directory_uri() . '/frontend/css/tiny-slider.css', false, '1.1', 'all');
wp_enqueue_style( 'frontendstyle', get_template_directory_uri() . '/frontend/css/style.css', false, '1.1', 'all');

//js
/* wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/frontend/js/bootstrap.bundle.min.js', array( 'jquery' ));
wp_enqueue_script( 'tinyslider', get_template_directory_uri() . '/frontend/js/tiny-slider.js', array( 'jquery' ));
wp_enqueue_script( 'custom', get_template_directory_uri() . '/frontend/js/custom.js', array( 'jquery' )); */
}
/**************** */
- add script at footer
- footer.php
****************
?php%20site_url();%20?/wp-content/themes/mytheme/frontend/js/bootstrap.bundle.min.js
*****************
2. Menu
- Install menudesign plugin
- add menu class sample
- in menuid
- menu1 //it is id of menu #menu1
- in menu class
- .menu-item-has-children //it is class of element . important need to add space as first letter
- in menu add class
- custom-navbar-nav navbar-nav ms-auto mb-2 mb-md-0
- frontend code
******************
<?php echo do_shortcode("[mymenu location='primary' menuid='menu1']"); ?>
*******************
3. Plugins Needed for Interview
- bootstrap
- menudesign
- misha image upload
- addwidget
- custom post
- mytheme Theme
- Third party plugins
- contactform 7
- additional header
- Reply-To: [your-email]
- from
- [_site_title] [your-email]
- Contact Form Entries
- Post SMTP
4. Create admin page and form
- Add admin menu code
/*****Add Main Menu with Seperate Page Script***/
add_action( 'admin_menu', 'my_form' );

function my_form() {
add_menu_page( 'Theme Options', 'My Form', 'manage_options', 'theme-options-form-element', 'theme_general_form', 'dashicons-admin-generic', 6 );
}
/*******End Script**********/

/************Admin Page Script***********/
function theme_general_form(){
?>
<form method="post" action="options.php">
<?php wp_nonce_field('update-options') ?>
<h1>Menu Element</h1>
<div class="panel panel-default">
<div class="panel-body">
<div class="well">

<table id="menuelementpolicyTable" class="table menuelementpolicyorder-list">
<thead>
<tr>
<td>Menuid</td>
</tr>
</thead>
<tbody>

<tr>
<td class="col-sm-4">
<label for="">Form Title</label>
<input required type="text" name="formtitle" class="form-control new-option" value="<?php echo get_option('formtitle');?>"/>
<label for="">Content</label>
<?php
/***********wysiwyg editor script******************/
$settings = array( 'media_buttons' => true,'quicktags' => true );
$content =get_option( 'formcontent' );
$editor_id = 'formcontent';
wp_editor( $content, $editor_id,$settings );
/******End Script********/
?>
<label for="">Image</label>
<?php echo misha_image_uploader_field('formimage',get_option('formimage'),'back') ?>
</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>

<p><input required type="submit" class="checkoptionsubmit" name="Submit" value="Save" /></p>
<input required type="hidden" name="action" value="update" />
<input required type="hidden" name="page_options" value="formtitle,formcontent,formimage" />
</form>
<?php
}
******************
5. misha imagae
- install misha image plugin
- backend code
- use front for frontend
*******************
<?php echo misha_image_uploader_field('formimage',get_option('formimage'),'back') ?>
*******************
- get misha image url
*******************
<?php
$image_attributes = wp_get_attachment_image_src( get_option('formimage'), 'full' );
?>
<img src="<?php echo $image_attributes[0];?>" class="img-responsive" style="display:block;" />
********************
6. Widgets
- install addwidget plugin
- widget name can include space eg : Footer One and call in code without space eg: dynamic_sidebar( 'footerone' );
- Footer widget code
***********************
<?php if ( is_active_sidebar( 'first' ) ) : ?>
<div class="col-lg-4">
<?php dynamic_sidebar( 'first' ); ?>
</div>
<?php endif; ?>
************************
7. Custom Post
- install custom post plugin
- featured image enable
********************
add_theme_support( 'post-thumbnails', array( 'post', 'page', 'post_type') );
*******************
- get featured image url
*******************
<?php echo get_the_post_thumbnail_url(get_the_ID(),'medium');?>
********************
- get custom field
******************
<?php echo get_post_meta( get_the_ID(), 'sub_title', true);?>
******************
- frontend listing
******************
<?php $loop = new WP_Query( array( 'post_type' => 'my_testimonialcustom', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title()?>
<?php the_content(); ?>
<?php endwhile; ?>
*******************
8. Archive page with pagination
- wp query listing
*******************
<?php
$myQuery = new WP_Query();

$cars = $myQuery->query([
'post_type' => 'my_testimonialcustom',
'posts_per_page' => 1,
'paged' => !empty($_GET['pg']) ? absint($_GET['pg']) : 1,
]);

foreach ($cars as $car) {
echo $car->post_title;
}

echo myPaginateLinks($myQuery);
?>
*********************
- functions.php add myPaginateLinks function
***********************
function myPaginateLinks( WP_Query $wp_query, $args = '' ) {
global $wp_rewrite;

// Setting up default values based on the current URL.
$pagenum_link = html_entity_decode( get_pagenum_link() );
$url_parts = explode( '?', $pagenum_link );

// Get max pages and current page out of the current query, if available.
$total = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1;
$current = !empty($_GET['pg']) ? absint($_GET['pg']) : 1;

// Append the format placeholder to the base URL.
$pagenum_link = trailingslashit( $url_parts[0] ) . '%_%';

// URL base depends on permalink settings.
$format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= '?pg=%#%';

$defaults = array(
'base' => $pagenum_link, // http://example.com/all_posts.php%_% : %_% is replaced by format (below).
'format' => $format, // ?page=%#% : %#% is replaced by the page number.
'total' => $total,
'current' => $current,
'aria_current' => 'page',
'show_all' => false,
'prev_next' => true,
'prev_text' => __( '&laquo; Previous' ),
'next_text' => __( 'Next &raquo;' ),
'end_size' => 1,
'mid_size' => 2,
'type' => 'plain',
'add_args' => array(), // Array of query args to add.
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => '',
);

$args = wp_parse_args( $args, $defaults );

if ( ! is_array( $args['add_args'] ) ) {
$args['add_args'] = array();
}

// Merge additional query vars found in the original URL into 'add_args' array.
if ( isset( $url_parts[1] ) ) {
// Find the format argument.
$format = explode( '?', str_replace( '%_%', $args['format'], $args['base'] ) );
$format_query = isset( $format[1] ) ? $format[1] : '';
wp_parse_str( $format_query, $format_args );

// Find the query args of the requested URL.
wp_parse_str( $url_parts[1], $url_query_args );

// Remove the format argument from the array of query arguments, to avoid overwriting custom format.
foreach ( $format_args as $format_arg => $format_arg_value ) {
unset( $url_query_args[ $format_arg ] );
}

$args['add_args'] = array_merge( $args['add_args'], urlencode_deep( $url_query_args ) );
}

// Who knows what else people pass in $args.
$total = (int) $args['total'];
if ( $total < 2 ) {
return;
}
$current = (int) $args['current'];
$end_size = (int) $args['end_size']; // Out of bounds? Make it the default.
if ( $end_size < 1 ) {
$end_size = 1;
}
$mid_size = (int) $args['mid_size'];
if ( $mid_size < 0 ) {
$mid_size = 2;
}

$add_args = $args['add_args'];
$r = '';
$page_links = array();
$dots = false;

if ( $args['prev_next'] && $current && 1 < $current ) :
$link = str_replace( '%_%', 2 == $current ? '' : $args['format'], $args['base'] );
$link = str_replace( '%#%', $current - 1, $link );
if ( $add_args ) {
$link = add_query_arg( $add_args, $link );
}
$link .= $args['add_fragment'];

$page_links[] = sprintf(
'<a class="prev page-numbers" href="%s">%s</a>',
/**
* Filters the paginated links for the given archive pages.
*
* @since 3.0.0
*
* @param string $link The paginated link URL.
*/
esc_url( apply_filters( 'paginate_links', $link ) ),
$args['prev_text']
);
endif;

for ( $n = 1; $n <= $total; $n++ ) :
if ( $n == $current ) :
$page_links[] = sprintf(
'<span aria-current="%s" class="page-numbers current">%s</span>',
esc_attr( $args['aria_current'] ),
$args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number']
);

$dots = true;
else :
if ( $args['show_all'] || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
$link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] );
$link = str_replace( '%#%', $n, $link );
if ( $add_args ) {
$link = add_query_arg( $add_args, $link );
}
$link .= $args['add_fragment'];

$page_links[] = sprintf(
'<a class="page-numbers" href="%s">%s</a>',
/** This filter is documented in wp-includes/general-template.php */
esc_url( apply_filters( 'paginate_links', $link ) ),
$args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number']
);

$dots = true;
elseif ( $dots && ! $args['show_all'] ) :
$page_links[] = '<span class="page-numbers dots">' . __( '&hellip;' ) . '</span>';

$dots = false;
endif;
endif;
endfor;

if ( $args['prev_next'] && $current && $current < $total ) :
$link = str_replace( '%_%', $args['format'], $args['base'] );
$link = str_replace( '%#%', $current + 1, $link );
if ( $add_args ) {
$link = add_query_arg( $add_args, $link );
}
$link .= $args['add_fragment'];

$page_links[] = sprintf(
'<a class="next page-numbers" href="%s">%s</a>',
/** This filter is documented in wp-includes/general-template.php */
esc_url( apply_filters( 'paginate_links', $link ) ),
$args['next_text']
);
endif;

switch ( $args['type'] ) {
case 'array':
return $page_links;

case 'list':
$r .= "<ul class='page-numbers'>\n\t<li>";
$r .= implode( "</li>\n\t<li>", $page_links );
$r .= "</li>\n</ul>\n";
break;

default:
$r = implode( "\n", $page_links );
break;
}

/**
* Filters the HTML output of paginated links for archives.
*
* @since 5.7.0
*
* @param string $r HTML output.
* @param array $args An array of arguments. See paginate_links()
* for information on accepted arguments.
*/
$r = apply_filters( 'paginate_links_output', $r, $args );

return $r;
}
************************

Leave a comment