Asked  8 Months ago    Answers:  5   Viewed   27 times

In my form I have the following values that are based on a standard PHP/MySql query.

echo "<tr>n
        <td align='right'><b>Location</b></td>
        <td><input name='student_location' type='text' size='25' style='font-weight: 700' value=$location></td>
    </tr>";

When the value of $location is a single word it displays properly, when it is more than one word say 'North Campus' only 'North' displays.

I've doubled and triple checked and the correct value is in the database, when I do an echo for the value of $location it echoes the correct value but when it's displayed in the field above it chops the last word. It's doing it to all of my variables that are more than one word, so I've missed something obvious.

 Answers

55

You forgot quotes :

echo "<tr>n
    <td align='right'><b>Location</b></td>
    <td><input name='student_location' type='text' size='25' style='font-weight: 700' value="$location"></td>
</tr>";

Without quotes, the first word will be noted, others will be interpreted as wrong attributes.

Wednesday, March 31, 2021
 
Wickethewok
answered 8 Months ago
91

Place a Custom-Post-Type in an submenu of an existing parent page

According to the Codex, this is a known and expected behavior:

Note: When using 'some string' to show as a submenu of a menu page created by a plugin, this item will become the first submenu item, and replace the location of the top level link.

Source: https://codex.wordpress.org/Function_Reference/register_post_type#Arguments (See the "show_in_menu" section)

Here is the end of the quote which offers a solution:

If this isn't desired, the plugin that creates the menu page needs to set the add_action priority for admin_menu to 9 or lower.

So this is quite simple to solve. However in my case I couldn't change the priority of the parent page because it is generated by a third-party library. Therefore I came up with this solution:

// Move the "example_cpt" Custom-Post-Type to be a submenu of the "example_parent_page_id" admin page.
add_action('admin_menu', 'fix_admin_menu_submenu', 11);
function fix_admin_menu_submenu() {

    // Add "Example CPT" Custom-Post-Type as submenu of the "Example Parent Page" page
    add_submenu_page('example_parent_page_id', 'Example CPT', 'Example CPT', 'edit_pages' , 'edit.php?post_type=example_cpt');
}

Please note the priority 11, and also when registering the Custom-Post-Type I set the "show_in_menu" parameter to false, so we can add it in the menu manually via add_submenu_page as shown above.


Properly set the Custom-Post-Type submenu entry as "active"

Now, the above solution works fine, however when creating/editing a post of the "example_cpt" Custom-Post-Type, it is not set as active and the submenu is not unfolded. Here is how to make sure that it is set as active, as well as the submenu in which it resides is properly set as active when creating/editing a post of the "example_cpt" Custom-Post-Type:

// Set the "example_parent_page_id" submenu as active/current when creating/editing a "example_cpt" post
add_filter('parent_file', 'fix_admin_parent_file');
function fix_admin_parent_file($parent_file){
    global $submenu_file, $current_screen;

    // Set correct active/current menu and submenu in the WordPress Admin menu for the "example_cpt" Add-New/Edit/List
    if($current_screen->post_type == 'example_cpt') {
        $submenu_file = 'edit.php?post_type=example_cpt';
        $parent_file = 'example_parent_page_id';
    }
    return $parent_file;
}

Fine-tuning: Rename the first submenu entry

Furthermore, I also wanted the first menu entry of my submenu to be named differently from the parent name. By default, and using the code above, this is what we have:

- Example Parent Page
-- Example Parent Page
-- Example CPT

So as you can see, the first menu entry of the submenu is a duplicate of the parent menu, and this is the default WordPress behavior. I wanted to rename this duplicate entry to something different, much like WordPress does with the default menus (for example "Posts" and the submenu entry "All Posts" which both point to the same page but are named differently).

Here is how to rename the first submenu entry:

add_action('admin_menu', 'rename_first_submenu_entry', 11);
function rename_first_submenu_entry() {

    // Rename first submenu entry (duplicate of parent menu) from "Example Parent Page" to "Submenu Text"
    add_submenu_page('example_parent_page_id', 'Example Parent Page', 'Submenu Text', 'edit_pages' , 'example_parent_page_id');

}

Please note the priority 11, so it is renamed after it has been created. And now we have:

- Example Parent Page
-- Submenu Text
-- Example CPT

Please note that "Submenu Text" points to the same location as "Example Parent Page".

Saturday, May 29, 2021
 
Lloydworth
answered 5 Months ago
23

That is easy.

You have to use two Wordpress functions.

add_rewrite_rule()

and

add_rewrite_tag()

First you include the add_rewrite_tag() function into your functions.php of your theme.

For Example:

function custom_rewrite_tag() {
  add_rewrite_tag('%food%', '([^&]+)');
  add_rewrite_tag('%variety%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);

After that you have to choice one page of wordpress which contains your content. You have to use the page ID of this page.

Than you include the add_rewrite_rule() in your functions.php.

For Example:

 function custom_rewrite_rule() {
    add_rewrite_rule('^nutrition/([^/]*)/([^/]*)/?','index.php?page_id=12&food=$matches[1]&variety=$matches[2]','top');
  }
  add_action('init', 'custom_rewrite_rule', 10, 0);

Notice!:

After this you have to go to your settings -> permalinks and only push the save button to clean your permalink cache.

IMPORTANT: Do not forget to flush and regenerate the rewrite rules database after modifying rules. From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes.

My example is from the codex page of wordpress:

https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

https://codex.wordpress.org/Rewrite_API/add_rewrite_tag

Saturday, May 29, 2021
 
HamidR
answered 5 Months ago
45

Based on your screenshot of Settings (Preferences on Mac) | Editor | Language Injections.

Please delete 3rd language injection rule from the bottom (the one for "div" -- that has "IDE" in Scope column).

That rule injects HTML into div tag which tells IDE to treat all other code (even PHP) inside such tag as HTML/plain text.

Thursday, August 5, 2021
 
Nil
answered 3 Months ago
Nil
80

A recent change in PHPMailer allows Debugoutput to be a closure, so you can make it do whatever you like, for example to collect all the debug output and emit it later:

$debug = '';
$mail->Debugoutput = function($str, $level) {
    $GLOBALS['debug'] .= "$level: $strn";
};
//...later
echo $debug;
Monday, August 16, 2021
 
samayo
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share