How to add class to new wordpress upload item in post?
how to add from automatic:
upload file nameV
class="fileuploadclass"
?
Just paste following code in your current theme function.php file.
if ( ! function_exists( 'auto_custom_class_add_for_media_attchment' ) ) :
function auto_custom_class_add_for_media_attchment( $html, $id ) {
$attachment = get_post( $id );
$mime_type = $attachment->post_mime_type;
// Here i added if condition only for pdf mine type, you can use whatever mime_type you require or remove condition for all.
if ( $mime_type == 'application/pdf' ) {
$src = wp_get_attachment_url( $id );
$html = '<a class="fileuploadclass" href="'. $src .'">'. $attachment->post_title .'</a>';
}
return $html;
}
endif;
add_filter('media_send_to_editor', 'auto_custom_class_add_for_media_attchment', 20, 3);
Related
Hello I'm not really into PHP. I want to add multiple advanced custom fields to post/product archive in Wordpress. Here is the code I have that works but it only adds one field. Any help will be greatly appreciated.
add_filter('nectar_post_grid_excerpt','salient_mod_post_grid_excerpt');
function salient_mod_post_grid_excerpt($excerpt) {
global $post;
$custom_meta = '';
// Grab custom ACF field value.
if( function_exists('get_field') && isset($post->ID) ) {
$custom_meta = get_field( 'my_field', $post->ID );
}
$custom_excerpt = $excerpt . $custom_meta;
return $custom_excerpt;
}
To add multiple advanced custom fields to the post/product archive in WordPress, you will need to use the same get_field function to grab the values of each field, and then concatenate them to the excerpt in the same way as you are doing with the current field. Here's an example of how you can add two more fields to the code you provided:
add_filter('nectar_post_grid_excerpt','salient_mod_post_grid_excerpt');
function salient_mod_post_grid_excerpt($excerpt) {
global $post;
$custom_meta = '';
// Grab custom ACF field values.
if( function_exists('get_field') && isset($post->ID) ) {
$custom_meta = get_field( 'my_field', $post->ID );
$custom_meta_2 = get_field( 'my_field_2', $post->ID );
$custom_meta_3 = get_field( 'my_field_3', $post->ID );
}
$custom_excerpt = $excerpt . $custom_meta . $custom_meta_2 . $custom_meta_3;
return $custom_excerpt;
}
Make sure to replace the field names ('my_field_2', 'my_field_3') with the actual field names of the custom fields you want to add.
Also, you can use a loop to add multiple fields dynamically.
$custom_meta = "";
if( function_exists('get_field') && isset($post->ID) ) {
for($i=1;$i<=3;$i++)
{
$custom_meta .= get_field( 'my_field_'.$i, $post->ID );
}
}
I'm looking to (programmatically) edit/alter the title of a page when its created and before its saved. I've tried a couple hooks to no avail. I can access the title being saved along with other info which ill be used to alter the title, but can not find the right call to save the altered title.
It is for a private/local lan wiki (currently MediaWiki version 1.38.1 ).
When a new article is created with in a certon category I want to number it with a prefix of #### - based on the number of articles already in the category. At the top of the category page itself I have a <inputbox> which pulls a template that has the wiki syntax for the category in it [[Category:BlaBla]]. When the article is saved I'm doing a check to make sure its a new article and some ofther checks for the info I need, which is working fine, but I can not save the new altered page name.
I've tried the following hooks to no avail.
onMultiContentSave
onArticlePrepareTextForEdit
https://www.mediawiki.org/wiki/Manual:Hooks/MultiContentSave
https://www.mediawiki.org/wiki/Manual:Hooks/ArticlePrepareTextForEdit
Heres acouple snippets ive been testing with, both do as i want, aside from saving the altered page name.
public static function onArticlePrepareTextForEdit( WikiPage $wikiPage, ParserOptions $parserOptions ) {
return;
$exists = $wikiPage->exists();
if ($exists == 1) {
#return true;
}
$getTitle = $wikiPage->getTitle();
# check if title starts with 0000, exit if so, no work needs to be done
if (self::titleCheck($getTitle)) {
#return true;
}
$checkCategories = $wikiPage->getCategories();
$inMalak = false;
foreach ($checkCategories as $value) {
if ($value == "Category:Malak") {
$inMalak = true;
}
}
if ($inMalak == 1) {
$newTitle = self::newTitlePre() . $getTitle;
#$wikiPage->setTitle($newTitle);
print(">" . $newTitle . "<br>");
}
self::pr($newTitle);
}
public static function onMultiContentSave(RenderedRevision $renderedRevision, UserIdentity $user, CommentStoreComment $summary, $flags, Status $hookStatus){
#return;
$revision = $renderedRevision->getRevision();
$getTitle = $revision->getPageAsLinkTarget();
if (self::titleCheck($getTitle)) {
return true;
}
#$titleOBJ = $revision->Title();
$title = $revision->getId();
$parent_id = $revision->getId();
$content = $revision->getContent( SlotRecord::MAIN );
$new_content = $content->getText();
#$test = $revision->ParserOutput();
$parent_id = "";
if ($parent_id == "") {
$pos = strpos($new_content, "[[Category:Malak]]");
if ($pos) {
$newTitle = self::newTitlePre() . $getTitle;
#$wikiPage->setTitle($newTitle);
}
}
self::pr($newTitle);
}
EDIT........
Still have not found the proper way to do this, but came up with a work around (hackery) which works for my needs.
Using the onEditFormPreloadText hook, change the url and added a new parameter ('MalakHere'), edited the 'title' parameter to the altered title, then do a redirect with the new page name. In the hook function there is a check for the 'MalakHere' parameter, if found (only cause of redirect) then it will exit the function so not to create a loop.
public static function onEditFormPreloadText(string &$text, Title &$title ) {
global $wgOut;
if ( isset( $_GET["MalakHere"] ) ) {
return true;
}
$pos = strpos($text, "[[Category:Malak]]");
if ($pos) {
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$urlTitle = urlencode($_GET["title"]);
$newURL = str_replace("title=" . $urlTitle,"MalakHere=yes",$url);
$newTitle = self::newTitlePre() . $title->prefixedText;
$url = $newURL . "&title=" . $newTitle;
return $wgOut->redirect($url);
}
return true;
}
Still have not found the proper way to do this, but came up with a work around (hackery) which works for my needs.
Using the onEditFormPreloadText hook, change the url and added a new parameter ('MalakHere'), edited the 'title' parameter to the altered title, then do a redirect with the new page name. In the hook function there is a check for the 'MalakHere' parameter, if found (only cause of redirect) then it will exit the function so not to create a loop.
public static function onEditFormPreloadText(string &$text, Title &$title ) {
global $wgOut;
if ( isset( $_GET["MalakHere"] ) ) {
return true;
}
$pos = strpos($text, "[[Category:Malak]]");
if ($pos) {
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$urlTitle = urlencode($_GET["title"]);
$newURL = str_replace("title=" . $urlTitle,"MalakHere=yes",$url);
$newTitle = self::newTitlePre() . $title->prefixedText;
$url = $newURL . "&title=" . $newTitle;
return $wgOut->redirect($url);
}
return true;
}
From a custom function to register users, I inserted this function to generate and overwrite the WordPress system email for new user notification.
function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
global $wpcargo;
$user_url = stripslashes( $user->user_url );
$user_login = stripslashes( $user->user_login );
$user_email = stripslashes( $user->user_email );
$user_firstname = stripslashes( $user->user_firstname );
$user_last_name = stripslashes( $user->user_last_name );
$user_pass = stripslashes( $user->user_pass );
$message = file_get_contents('../email/mail-template.php');
$wp_new_user_notification_email['subject'] = sprintf( '[%s] Welcome.', $blogname );
$wp_new_user_notification_email['headers'] = array('Content-Type: text/html; charset=UTF-8');
$wp_new_user_notification_email['message'] = $message;
return $wp_new_user_notification_email;
}
add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 10, 3 );
Emails are being sent but I cannot get user data printed in the PHP / HTML file.
<p style="margin-top:0;margin-bottom:12px;"><b>Name</b>: <?php $user_firstname; ?></p>
I'm wrong with the code, any suggestions?
I would use locate_template for the email contents and output buffering rather than file_get_contents - also as #Zak pointed out, you have to echo your output otherwise it won't show up.
function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
global $wpcargo;
$user_url = stripslashes( $user->user_url );
$user_login = stripslashes( $user->user_login );
$user_email = stripslashes( $user->user_email );
$user_firstname = stripslashes( $user->user_firstname );
$user_last_name = stripslashes( $user->user_last_name );
$user_pass = stripslashes( $user->user_pass );
ob_start();
include( locate_template( '/email/mail-template.php' ); // This path may vary depending on your setup.
$wp_new_user_notification_email['message'] = ob_get_clean();
$wp_new_user_notification_email['subject'] = sprintf( '[%s] Welcome.', $blogname );
$wp_new_user_notification_email['headers'] = array('Content-Type: text/html; charset=UTF-8');
return $wp_new_user_notification_email;
}
add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 10, 3 );
Within your template you want to make sure you echo and esc_html() for anything that is being outputted.
<?php echo esc_html( $user_firstname ); ?>
I have a little problem. I have html page and I want to convert to pdf. My index page has a list that will get to the database and click on "Download PDF", I put this list in a PDF file.
My controller:
<?php
class pdf_c extends CI_Controller{
function __construct() {
parent::__construct();
$this->load->helper(array('url', 'mediatutorialpdf'));
}
function index($download_pdf = ''){
$ret = '';
$ID = 1;
$pdf_filename = 'user_info_'.$ID.'.pdf';
$link_download = ($download_pdf == TRUE)?'':anchor(base_url().'index.php/true', 'Download PDF');
$query = $this->db->query("SELECT * FROM `ci_pdf_user` WHERE `id` = '{$ID}' LIMIT 1");
if($query->num_rows() > 0)
{
$user_info = $query->row_array();
}
$data_header = array(
'title' => 'Convert codeigniter to pdf'
);
$data_userinfo = array(
'user_info' => $user_info,
'link_download' => $link_download
);
$header = $this->load->view('header',$data_header, true);
$user_info = $this->load->view('user_table', $data_userinfo, true);
$footer = $this->load->view('footer','', true);
$output = $header.$user_info.$footer;
if($download_pdf == TRUE)
{
generate_pdf($output, $pdf_filename);
}
else
{
echo $output;
}
}
}
?>
The problem is when I click the button "Download PDF" should redirect me to the function index () and get the $ download_pdf = true. And so called generate_pdf function () that will generate the PDF.
I think the problem is in the variable $ link_download, but can not solve the problem.
Thanks
I think that you could try with:
function index(pdf = 0)...
Then check that optional parameter with:
$pdf = $this->uri->segment(2, 0); //not sure, should be 2? try it...`
And then, if $pdf=='1' (send nummber rather than string 'true') ...etc,etc...
My mediawiki version is 1.16.5. I have a function that selects a random quote from the database and displays it on a wiki page. Italics and bold are rendered when I use html, however, I want to be able to use wikitext, specifically external links format, i.e.
[http://mediawiki.org MediaWiki]
Currently, if using something like the above, it is not rendered and displays literally. There is some reference on what is needed here: http://www.mediawiki.org/wiki/Manual:Tag_extensions#How_do_I_render_wikitext_in_my_extension.3F but I do not know how to implement this.
Here is the code:
function wfGetQuote() {
$randstr = wfRandom();
$row = selectRandomQuoteFromDB( $randstr );
if( !$row )
$row = selectRandomQuoteFromDB( "0" );
if( $row ) {
list( $quote, $attribution ) = explode( "\n", $row->quote_text );
return '<div id="trrandomquote"><div id="trquote">'. $quote .'</div> <span>'. $attribution .'</span></div>';
}
else
return 'Error: No quote found';
}
function selectRandomQuoteFromDB( $randstr ) {
global $wgUser, $site;
$lang = $wgUser->getOption( 'language' );
if( $site == 'wiki' )
$lang = 'en';
$dbr = wfGetDB( DB_SLAVE );
$use_index = $dbr->useIndexClause( 'quote_random' );
$quote = $dbr->tableName( 'quote' );
$sql = "SELECT quote_text
FROM $quote $use_index
WHERE quote_random >= $randstr
AND quote_lang = '$lang'";
$sql = $dbr->limitResult( $sql, 1, 0 );
$fname = 'RandomQuote::selectRandomQuoteFromDB';
$res = $dbr->query( $sql, $fname );
return $dbr->fetchObject( $res );
}
Normally I use the $wgOut->addWikiText( $msg ); function.
I would modify your code to:
function wfGetQuote() {
global $wgOut; // Added line
$randstr = wfRandom();
$row = selectRandomQuoteFromDB( $randstr );
if( !$row )
$row = selectRandomQuoteFromDB( "0" );
if( $row ) {
list( $quote, $attribution ) = explode( "\n", $row->quote_text );
$wgOut->addWikiText( '<div id="trrandomquote"><div id="trquote">'. $quote .'</div> <span>'. $attribution .'</span></div>' );
}
else
return 'Error: No quote found';
}
I haven't tested it, but it may do the job you're looking for.