Functions.php Wordpress syntax error - javascript

I'm implementing Wordpress (from scratch) in a custom design and I'm having some issues with the functions.php file.
I don't have any plugins added to my website that current hosted locally with MAMP.
There are three parts of the code: one to create custom size of thumbnails, another to fix a media library error and another to create 3 different sidebar widgets.
I added the first two sections (thumbnail + media library) and everything worked fine, but after adding the last part (sidebar) the media library doesn't work again. When deleting the sidebar code, the media library works again. So these three parts are interfering and canceling each other out somehow.
I'm pretty sure my mistake is of syntax... Here is the full code:
<?php
// Add custom size of thumbnails
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 150, 150, true ); // default Post Thumbnail dimensions (cropped)
add_image_size( 'another-size', 600, 400 ); // Not Cropped (additional sizes) size doesn't matter because we're adding "full"
}
// Fixes Media Library error (without this it's not possible to add images or manage it – it just appears empty)
function ms_image_editor_default_to_gd( $editors ) {
$gd_editor = 'WP_Image_Editor_GD';
$editors = array_diff( $editors, array( $gd_editor ) );
array_unshift( $editors, $gd_editor );
return $editors;
}
add_filter( 'wp_image_editors', 'ms_image_editor_default_to_gd' );
?>
<?php
// Created three different sidebars and allows user to edit their content as widgets
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'column 1',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
register_sidebar(array(
'name' => 'column 2',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
register_sidebar(array(
'name' => 'column 3',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
}?>

Try this, maybe you should use only one <?php ?> section
<?php
// Add custom size of thumbnails
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 150, 150, true ); // default Post Thumbnail dimensions (cropped)
add_image_size( 'another-size', 600, 400 ); // Not Cropped (additional sizes) size doesn't matter because we're adding "full"
}
// Fixes Media Library error (without this it's not possible to add images or manage it – it just appears empty)
function ms_image_editor_default_to_gd( $editors ) {
$gd_editor = 'WP_Image_Editor_GD';
$editors = array_diff( $editors, array( $gd_editor ) );
array_unshift( $editors, $gd_editor );
return $editors;
}
add_filter( 'wp_image_editors', 'ms_image_editor_default_to_gd' );
// Created three different sidebars and allows user to edit their content as widgets
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'column 1',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
register_sidebar(array(
'name' => 'column 2',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
register_sidebar(array(
'name' => 'column 3',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
}?>

Related

Remove products from category when status is "in-stock"

I need a little help. I need to hide category which will contain few out-of-stock products and I get it done with this code
function custom_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'u-dolasku' ), // Don't display products in the clothing category on the shop page.
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
Now I have a question, is it possible to remove product from that category when stock is updated, exactly when stock status go from "out-of-stock" to "in-stock" ?

how to link Javascript function with wordpress' customize control

I have added a new section in customize section
function select_theme_stylesheet( $wp_customize ){
$wp_customize->add_section( 'theme_stylesheet_selector', array(
'title' => 'Select Color',
'priorty' => 50
));
$wp_customize->add_setting( 'theme_stylesheet_settings', array(
'default' => 'green',
'transport' => 'postMessage'
));
$wp_customize->add_control( new WP_Customize_Control( $wp_customize,'theme_stylesheet_control',array(
'type' => 'radio',
'label' => 'Theme Scheme',
'section' => 'theme_stylesheet_selector',
'settings' => 'theme_stylesheet_settings',
'choices' => array(
'green',
'blue'
),
'selection' => 'colors',
'priorty' => 9
) ));
}
add_action('customize_register', 'select_theme_stylesheet');
And I have the following function in 'customize.preview.js'
wp.customize( 'theme_stylesheet_control', function( value ) {
value.bind( function( newval ) {
alert("hello" + newval);
} );
} );
BUT It's not triggering. Can anyone please tell me what I'm missing.

Add WordPress wysiwyg/tiny mce editor on an image attachment details

Right now I have this snippet of code, and can add an input on the attachment details of an image. But I don't know how to add a wysiwyg/tinymce editor like the one in the widgets: https://gyazo.com/d40dad527f9d7b52f112b6884aac009c. Am I going to have to add some javascript in the admin.js to turn this textarea into a tinymce editor?
add_filter( 'attachment_fields_to_edit', [self::class, 'test'], 10, 2 );
function test($fields, $post) {
$meta = get_post_meta( $post->ID, 'buy_now', true );
$fields['test'] = array(
'label' => __( 'Test', 'text-domain' ),
'input' => 'textarea',
'value' => $meta,
'show_in_edit' => true,
);
return $fields;
}

Custom Post Type - Category Add Button Not Working

I created a custom post type and also a custom taxonomy for that post type. Everything seems to be working... except when I go to create a new post, if I click on "+ Add New Category" it changes the URL, but nothing happens. All the other JS buttons work and it works just fine on regular posts.. not sure why it is doing this. Below is the codes i am using the the functions.php file.
Register Custom Post Type
/* Custom Post Type Galleries */
function bhgallery_register_post_type() {
$singular = 'Gallery';
$plural = 'Galleries';
$labels = array (
'name' => $plural,
'singular' => $singular,
'add_name' => 'Create New',
'add_new_item' => 'Create New ' . $singular,
'edit' => 'Edit',
'edit_item' => 'Edit ' . $singular,
'new_item' => 'New' . $singular,
'view' => 'View' . $singular,
'view_item' => 'View' . $singular,
'search_term' => 'Search ' . $plural,
'parent' => 'Parent ' . $singular,
'not_found' => 'No ' . $plural . ' Found',
'not_found_in_trash' => 'No ' . $plural . ' in Trash'
);
$args = array (
'labels' => $labels,
'public' => true,
'public_queryable' => true,
'exclude_from_search' => false,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 10,
'menu_icon' => 'dashicons-camera',
'can_export' => true,
'delete_with_user' => false,
'hierarchical' => false,
'has_archive' => true,
'query_var' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
// 'capabilities' => array();
'rewrite' => array(
'slug' => 'gallery',
'with_front' => true,
'pages' => true,
'feeds' => false,
),
'supports' => array(
'title',
'thumbnail',
'editor'
)
);
register_post_type( 'bh_gallery', $args );
}
add_action ( 'init', 'bhgallery_register_post_type');
CUSTOM TAXONOMY
/** Custom Categories for Gallery **/
function bhgallery_register_taxonomy() {
$plural = 'Categories';
$singular = 'Category';
$labels = array (
'name' => $plural,
'singular_name' => $singular,
'search_items' => 'Search ' . $plural,
'popular_items' => 'Popular ' . $plural,
'all_items' => 'All ' . $plural,
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Edit ' . $singular,
'update_item' => 'Update ' . $singular,
'add_new_item' => 'Add New ' . $singular,
'new_item_name' => 'New ' . $singular . ' Name',
'separate_items_with_comas' => 'Seperate ' . $singular . ' with commas',
'add_or_remove_items' => 'Add or remove ' . $plural,
'choose_from_most_used' => 'Choose from the most used ' . $plural,
'not_found' => 'No ' . $plural . 'fount',
'menu_name' => $plural,
);
$args = array (
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'categories'),
);
register_taxonomy( 'gallery category', 'bh_gallery', $args );
}
add_action ( 'init', 'bhgallery_register_taxonomy');
If there is anything else you need to know, let me know.
Your second to last line of code above has
register_taxonomy( 'gallery category', 'bh_gallery', $args );
From the codex, register taxonomy, the first argument should not have any spaces. In fact, there are very few places that spaces are ever allowed and they are mainly limited to strings meant for display.
$taxonomy (string) (required) The name of the taxonomy. Name should
only contain lowercase letters and the underscore character, and not
be more than 32 characters long (database structure restriction).

wordpress custom query - orderby title will not work

I am having a problem getting a custom query to alphabetize. It keeps defaulting to displaying in the order of the date it was posted. Below is my php function.
function json_info2() {
// The $_REQUEST contains all the data sent via ajax
if ( isset($_REQUEST) ) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// get values for all three drop-down menus
$status = $_REQUEST['status'];
$industry = $_REQUEST['services'];
$state = $_REQUEST['state'];
// array of values for each of the three drop-downs
$statusAll = array('complete','incomplete');
$industryAll = array('mining','textile','machinery');
$statesAll = array('SC','TX','WA');
// set $statusArray dependent on whether or not "all" is selected in the dropdown menu
if($status == "all") {
$statusArray = array( 'key' => 'status', 'value' => $statusAll, 'compare' => 'IN');
} else {
$statusArray = array( 'key' => 'status', 'value' => $status, 'compare' => '=');
}
if($industry == "all") {
$industryArray = array( 'key' => 'industry', 'value' => $industryAll, 'compare' => 'IN');
} else {
$industryArray = array( 'key' => 'industry', 'value' => $industry, 'compare' => '=');
}
if($state == "all") {
$stateArray = array( 'key' => 'state', 'value' => $statesAll, 'compare' => 'IN');
} else {
$stateArray = array( 'key' => 'state', 'value' => $state, 'compare' => '=');
}
$pages = array(
'post_type' => 'page',
'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged,
'posts_per_page' => 5,
'meta_query' => array(
'relation' => 'AND',
$statusArray,
$industryArray,
$stateArray,
array(
'key' => '_wp_page_template',
'value' => 'template-individual-project.php',
'compare' => '='
)
)
);
// query results by page template
$my_query = new WP_Query($pages);
if($my_query->have_posts()) :
while($my_query->have_posts()) :
$my_query->the_post();
<li>
<?php the_title(); ?>
</li>
<?php
endwhile;endif;
wp_reset_query();
} // end of isset
?>
<?php
die();
}
add_action( 'wp_ajax_json_info2', 'json_info2' );
add_action( 'wp_ajax_nopriv_json_info2', 'json_info2' );
?>
This above function is called by the ajax function that follows:
function do_ajax() {
// Get values from all three dropdown menus
var state = $('#states').val();
var markets = $('#markets').val();
var services = $('#services').val();
$.ajax({
url: ajaxurl,
data: {
'action' : 'json_info2',
'state' : state,
'status' : markets,
'services' : services
},
success:function(moredata) {
// This outputs the result of the ajax request
$('#project-list').html( moredata );
$('#project-list').fadeIn();
}/*,
error: function(errorThrown){
var errorMsg = "No results match your criteria";
$('#project-list').html(errorMsg);
}*/
}); // end of ajax call
} // end of function do_ajax
Is there something simple that I'm missing here? I have a similar custom query on the page when it loads (although that initial load query doesn't have the select menu values as args), and they display in alphabetical order just fine. It's only after the ajax call to filter the list that they are no longer in order.
I have found the issue after googling the problem for quite a while. I read that some of the people who were having this problem found that their theme was using a plugin called Post Types Order. It overrides the ability to set the orderby arg.
I looked at the plugins, and sure enough, Post Types Order was there. Everything I read said that the problem could be solved by unchecking "auto sort" in the settings for the plugin. However, I did that, and orderby still didn't work. I had to completely deactivate the plugin to get orderby title to work.

Categories

Resources